8000 refactor 206 · saurabh-deochake/Leetcode@94c6068 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94c6068

Browse files
refactor 206
1 parent 7f584f7 commit 94c6068

File tree

2 files changed

+54
-45
lines changed

2 files changed

+54
-45
lines changed

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

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,53 @@
88
* Reverse a singly linked list.*/
99
public class _206 {
1010

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*/
16-
public ListNode reverseList_iterative(ListNode head) {
17-
/**It works out the best to set up a debug point and visualize this process:
18-
* e.g. 1->2->3-null
19-
* at the end of the first iteration of the while loop, the status is like this:
20-
* newHead: 1->null
21-
* head: 2->3-null
22-
* then it continues the iteration.*/
23-
ListNode newHead = null;
24-
while (head != null) {
25-
ListNode next = head.next;
26-
head.next = newHead;
27-
newHead = head;
28-
head = next;
11+
public static class Solution1 {
12+
/**
13+
* creating a newHead = null is a very common/smart way to handle such cases, the logic flows out very naturally:
14+
* create a new node called "next" to hold current head's next node
15+
* then we could redirect head's next pointer to point to newHead which is head's previous node
16+
* the above two steps finished the reversion, to continue this process until we reach the end of the original list,
17+
* we'll assign current "head" to new "newHead", and current "next" to be new "head" for the next iteration, here's the code
18+
*/
19+
public ListNode reverseList(ListNode head) {
20+
/**It works out the best to set up a debug point and visualize this process:
21+
* e.g. 1->2->3-null
22+
* at the end of the first iteration of the while loop, the status is like this:
23+
* newHead: 1->null
24+
* head: 2->3-null
25+
* then it continues the iteration.*/
26+
ListNode newHead = null;
27+
while (head != null) {
28+
ListNode next = head.next;
29+
head.next = newHead;
30+
newHead = head;
31+
head = next;
32+
}
33+
return newHead;
2934
}
30-
return newHead;
3135
}
3236

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-
*/
37-
public ListNode reverseList_recursive(ListNode head) {
38-
ListNode newHead = null;
39-
return reverse(head, newHead);
40-
}
37+
public static class Solution2 {
38+
/**
39+
* following the above iterative version, the recursive solution flows out so naturally:
40+
* basically, we just replaced the while loop with a recursive function
41+
* still, a null newHead proves to be very helpful.
42+
*/
43+
public ListNode reverseList(ListNode head) {
44+
ListNode newHead = null;
45+
return reverse(head, newHead);
46+
}
4147

42-
ListNode reverse(ListNode head, ListNode newHead) {
43-
if (head == null) {
44-
return newHead;
48+
ListNode reverse(ListNode head, ListNode newHead) {
49+
if (head == null) {
50+
return newHead;
51+
}
52+
ListNode next = head.next;
53+
head.next = newHead;
54+
newHead = head;
55+
head = next;
56+
return reverse(head, newHead);
4557
}
46-
ListNode next = head.next;
47-
head.next = newHead;
48-
newHead = head;
49-
head = next;
50-
return reverse(head, newHead);
5158
}
5259

5360
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package com.fishercoder;
22

33
import com.fishercoder.common.classes.ListNode;
4-
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.common.utils.LinkedListUtils;
55
import com.fishercoder.solutions._206;
66
import org.junit.BeforeClass;
77
import org.junit.Test;
88

9+
import static junit.framework.TestCase.assertEquals;
10+
911
/**
1012
* Created by stevesun on 6/5/17.
1113
*/
1214
public class _206Test {
13-
private static _206 test;
14-
private static ListNode actual;
15+
private static _206.Solution1 solution1;
16+
private static _206.Solution2 solution2;
1517
private static ListNode head;
1618

1719
@BeforeClass
1820
public static void setup() {
19-
test = new _206();
21+
solution1 = new _206.Solution1();
22+
solution2 = new _206.Solution2();
2023
}
2124

2225
@Test
2326
public void test1() {
24-
head = new ListNode(1);
25-
head.next = new ListNode(2);
26-
head.next.next = new ListNode(3);
27-
CommonUtils.printList(head);
28-
actual = test.reverseList_iterative(head);
29-
CommonUtils.printList(actual);
27+
head = LinkedListUtils.contructLinkedList(new int[]{1,2,3});
28+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{3,2,1}), solution1.reverseList(head));
29+
30+
head = LinkedListUtils.contructLinkedList(new int[]{1,2,3});
31+
assertEquals(LinkedListUtils.contructLinkedList(new int[]{3,2,1}), solution2.reverseList(head));
3032
}
3133
}

0 commit comments

Comments
 (0)
0