Golang iterative dummy pointer


  • 0
    R
    
    func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
        var dummy ListNode
        tail := &dummy
        
        for l1 != nil && l2 != nil {
            if l1.Val < l2.Val {
                tail.Next = l1
                l1 = l1.Next
            } else {
                tail.Next = l2
                l2 = l2.Next
            }
            tail = tail.Next
        }
        if l1 != nil {
            tail.Next = l1
        } else {
            tail.Next = l2
        }
        return dummy.Next
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.