Skip to content

Commit 482edc6

Browse files
committedAug 5, 2017
edit 206
1 parent 1e37b97 commit 482edc6

File tree

1 file changed

+21
-25
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+21
-25
lines changed
 

‎src/main/java/com/fishercoder/solutions/_206.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import com.fishercoder.common.classes.ListNode;
44

5-
/**Reverse a singly linked list.*/
5+
/**
6+
* 206. Reverse Linked List
7+
*
8+
* Reverse a singly linked list.*/
69
public class _206 {
7-
//creating a newHead = null is a very common/smart way to handle such cases, the logic flows out very naturally:
8-
//create a new node called "next" to hold current head's next node
9-
//then we could redirect head's next pointer to point to newHead which is head's previous node
10-
//the above two steps finished the reversion, to continue this process until we reach the end of the original list,
11-
//we'll assign current "head" to new "newHead", and current "next" to be new "head" for the next iteration, here's the code
10+
11+
/**creating a newHead = null is a very common/smart way to handle such cases, the logic flows out very naturally:
12+
create a new node called "next" to hold current head's next node
13+
then we could redirect head's next pointer to point to newHead which is head's previous node
14+
the above two steps finished the reversion, to continue this process until we reach the end of the original list,
15+
we'll assign current "head" to new "newHead", and current "next" to be new "head" for the next iteration, here's the code*/
1216
public ListNode reverseList_iterative(ListNode head) {
1317
/**It works out the best to set up a debug point and visualize this process:
1418
* e.g. 1->2->3-null
@@ -26,30 +30,22 @@ public ListNode reverseList_iterative(ListNode head) {
2630
return newHead;
2731
}
2832

29-
//following the above iterative version, the recursive solution flows out so naturally, basically, we just replaced the while loop with a recursive function
30-
//still, a null newHead proves to be very helpful.
33+
/**
34+
* following the above iterative version, the recursive solution flows out so naturally, basically, we just replaced the while loop with a recursive function
35+
* still, a null newHead proves to be very helpful.
36+
*/
3137
public ListNode reverseList_recursive(ListNode head) {
3238
ListNode newHead = null;
3339
return reverse(head, newHead);
3440
}
3541

36-
private ListNode reverse(ListNode head, ListNode newHead) {
37-
if (head != null) {
38-
ListNode next = head.next;
39-
head.next = newHead;
40-
newHead = head;
41-
head = next;
42-
return reverse(head, newHead);
43-
} else return newHead;
44-
}
45-
46-
//the above recursive function could of course be shortened to below, but the above one is just more intuitive and easier to follow and sort out your logic
47-
private ListNode reverse_more_concise(ListNode head, ListNode newHead) {
48-
if (head != null) {
49-
ListNode next = head.next;
50-
head.next = newHead;
51-
return reverse_more_concise(next, head);
52-
} else return newHead;
42+
ListNode reverse(ListNode head, ListNode newHead) {
43+
if (head == null) return newHead;
44+
ListNode next = head.next;
45+
head.next = newHead;
46+
newHead = head;
47+
head = next;
48+
return reverse(head, newHead);
5349
}
5450

5551
}

0 commit comments

Comments
 (0)