func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{}
prev := dummy
for l1 != nil && l2 != nil {
if l1.Val < l2.Val {
prev.Next = l1
l1 = l1.Next
} else {
prev.Next = l2
l2 = l2.Next
}
prev = prev.Next
}
if l1 != nil {
prev.Next = l1
} else {
prev.Next = l2
}
return dummy.Next
}
作者:xilepeng
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/golang-by-xilepeng/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
有疑问加站长微信联系(非本文作者)