206. Reverse Linked List
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 27, 2024
Last updated : June 27, 2024
Related Topics : Linked List, Recursion
Acceptance Rate : 77.65 %
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
head = helper(null, head);
return head;
}
private ListNode helper(ListNode prev, ListNode curr) {
if (curr == null) {
return prev;
}
ListNode output = helper(curr, curr.next);
curr.next = prev;
return output;
}
}