An intuitive solution without recursive method.


  • 0
    X
    func reverseList(head *ListNode) *ListNode {
        if head == nil {
            return head
        }
        # before record the current node's parent node before reversived
        var before *ListNode
        for head != nil {
            # First, record the next node
            # Second, change the current node.Next pointer to before node
            # Last, set current node to the next one
            temp := head.Next
            head.Next = before
            before = head
            head = temp
        }
       # After itering all the list, the head should be nil 
       # and the before node is the last node in the list before reversived
        return before
    }
    

Log in to reply
 

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