Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

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
}

LC 86. Partition List 分隔链表

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

  • No labels