题目:输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
type Node struct {
val int
next *Node
}
func union(head1 *Node, head2 *Node) *Node {
if head1 == nil {
return head2
}
if head2 == nil {
return head1
}
head := &Node{}
h := head
p, q := head1, head2
for p != nil && q != nil {
if p.val < q.val {
h.next = p
h = p
p = p.next
} else {
h.next = q
h = q
q = q.next
}
}
if q == nil {
h.next = p
}
if p == nil {
h.next = q
}
return head.next
}