recursive reverse linked list 1

recursive reverse linked list

public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
}

Here is what the above code is Doing:
1. If the head is null or the next node is null, return the head.
2. Set a pointer to the reversed list.
3. Set the next node’s next to the head.
4. Set the head’s next to null.
5. Return the pointer.

Similar Posts