-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
public class ReverseLinkedList_LC206 { | ||
public ListNode reverseList(ListNode head) { | ||
// return reverseIterative(head); | ||
return reverseRecursive(head); | ||
} | ||
|
||
/** | ||
* Keep three pointers at a node, it's previous, itself and its next. Iterate until end/null is reached | ||
* In every iteration, save copy of next, make current's next point to previous and update previous and current | ||
* | ||
* Time Complexity: O(N) N:number of nodes | ||
* Space Complexity: O(1) | ||
* | ||
* @param node | ||
* @return | ||
*/ | ||
ListNode reverseIterative(ListNode node) { | ||
ListNode previous = null, current = node, temp = null; | ||
while (current != null) { | ||
temp = current.next; | ||
current.next = previous; | ||
previous = current; | ||
current = temp; | ||
} | ||
return previous; | ||
} | ||
|
||
/** | ||
* With recursive function, reach all the way down to last node which would be head for reversedlist | ||
* Once end is reached, keep returning and update current node such that returned and reversed nodes point to | ||
* the current node. Change pointers while returning from each recursive call | ||
* | ||
* Time Complexity: O(N) N:number of nodes | ||
* Space Complexity: O(N) Stack space | ||
* | ||
* @param node | ||
* @return | ||
*/ | ||
ListNode reverseRecursive(ListNode node) { | ||
if (node == null || node.next == null) | ||
return node; | ||
|
||
ListNode head = reverseRecursive(node.next); | ||
node.next.next = node; | ||
node.next = null; | ||
return head; | ||
} | ||
} | ||
|
||
class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
ListNode() { | ||
} | ||
|
||
ListNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
ListNode(int val, ListNode next) { | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} |