This question is actually very useful for latter questions.


  • 0

    Actually quite amount of latter questions need this basic knowledge reverse a linked list. This is my .... I don't remember how many times I have written this simple short piece of code.

    public class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode newHead = null;
            while (head != null) {
                ListNode next = head.next;
                head.next = newHead;
                newHead = head;
                head = next;
            }
            return newHead;
        }
    }
    

Log in to reply
 

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