6 lines 0ms iterative JAVA solution


  • 0
    L

    Using three pointers to manipulate the list and do reverse in-place while traversing through the original list.

     public ListNode reverseList(ListNode head) {
                
                ListNode curr_node = head, next_node = null, reverse_head = null;
                
                while(curr_node!=null){
                    next_node = curr_node.next;
                    curr_node.next = reverse_head;
                    reverse_head = curr_node;
                    curr_node = next_node;
                }
                
                return reverse_head;
            }

Log in to reply
 

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