Reverse Linked List

Reverse a linked list. Do it in-place and in one-pass.

For example: Given 1->2->3->4->5->NULL,

return 5->4->3->2->1->NULL.

public class Solution {
    public ListNode reverseList(ListNode A) {
        ListNode prev = null, current = A;
        while (current.next != null) {
            ListNode temp = current.next;
            current.next = prev;
            prev = current;
            current = temp;
        }
        current.next = prev;
        return current;
    }
}

Last updated