&此题为使用 链表/ Linked List 求解的典型&
21. 合并两个有序链表
难度: 简单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
var dummy *ListNode = &ListNode{}
var curr *ListNode = dummy
for l1 != nil && l2 != nil {
if l1.Val < l2.Val { curr.Next = &ListNode{Val:l1.Val} l1 = l1.Next } else {
curr.Next = &ListNode{Val:l2.Val} l2 = l2.Next
}
curr = curr.Next
}
if l1 != nil { curr.Next = l1 }
if l2 != nil { curr.Next = l2 }
return dummy.Next }
|
原文链接: https://dashen.tech/2015/03/01/leetcode-21-合并两个有序链表/
版权声明: 转载请注明出处.