[C#] Simple fast (100%) solution using constant memory


  • 1
    N
    public ListNode ReverseList(ListNode head) {
        if (head == null) { return null; }
        ListNode lastNode = head;
        ListNode currentNode = head.next;
        while (currentNode != null) {
            ListNode nextNode = currentNode.next;
            currentNode.next = lastNode;
            lastNode = currentNode;
            currentNode = nextNode;
        }
        head.next = null;            
        return lastNode;
    }

Log in to reply
 

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