LC 21. Merge Two Sorted Lists 合并两个有序链表
https://leetcode.com/problems/merge-two-sorted-lists/
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { dummy := &ListNode{} h := dummy h1, h2 := list1, list2 for h1 != nil && h2 != nil { if h1.Val < h2.Val { h.Next = h1 h1 = h1.Next h = h.Next } else { h.Next = h2 h2 = h2.Next h = h.Next } } for h1 != nil { h.Next = h1 h1 = h1.Next h = h.Next } for h2 != nil { h.Next = h2 h2 = h2.Next h = h.Next } return dummy.Next }