-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathReverse Link List Recursion_new.java
More file actions
55 lines (48 loc) · 1.73 KB
/
Reverse Link List Recursion_new.java
File metadata and controls
55 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Problem: Reverse a linked list using recursion.
* Algorithm: Recursive approach - reach end of list, then reverse by modifying links.
* Time Complexity: O(n) where n is the number of nodes
* Space Complexity: O(n) for recursion stack depth
*/
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
/**
* Reverses a linked list using recursion.
* Example: 1 -> 2 -> 3 becomes 3 -> 2 -> 1
*
* @param A the head of the linked list
* @return the new head of the reversed list
*/
public ListNode reverseList(ListNode A) {
// Step 1: Base case - if list is empty, return null
if (A == null) {
return A;
}
// Step 2: Store next node before we modify A
ListNode rest = A.next;
// Step 3: Base case - if current node is last node, return it
// This becomes the new head of reversed list
if (rest == null) {
return A;
}
// Step 4: Disconnect current node from the rest
// This prevents cycles when we reverse
A.next = null;
// Step 5: Recursively reverse the rest of the list
// rest will be the head of already-reversed sublist
ListNode reverse = reverseList(rest);
// Step 6: Connect the rest of the list back to current node
// After recursion, 'rest' points to what was a middle/end node
// Now we make 'rest' point back to 'A' to reverse the link
rest.next = A;
// Step 7: Return the new head (unchanged in recursion)
return reverse;
}
}