Skip to content

Latest commit

 

History

History
52 lines (42 loc) · 1.16 KB

_206. Reverse Linked List.md

File metadata and controls

52 lines (42 loc) · 1.16 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 27, 2024

Last updated : June 27, 2024


Related Topics : Linked List, Recursion

Acceptance Rate : 77.65 %


Solutions

Java

/**
 * 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; 
    }
}