Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Autosaved
Table of Contents
stylenone

LC 21. Merge Two Sorted Lists 合并两个有序链表

https://leetcode.com/problems/merge-two-sorted-lists/

Code Block
languagego
/**
 * 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
}

LC 86. Partition List 分隔链表

https://leetcode.com/problems/partition-list/