Example: Reverse Linked List ‐ Recursive - rFronteddu/general_wiki GitHub Wiki
Given the head of a singly linked list, reverse the list, and return the reversed list.
class Solution {
public ListNode reverseList (ListNode head) {
// recursive
return reverse (head);
}
public ListNode reverse (ListNode head) {
if (head == null || head.next == null) {
return head;
}
var newHead = reverse(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
}