Skip to content

Commit b5de54e

Browse files
authored
Added tasks 141-143.
1 parent d4c2419 commit b5de54e

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0101_0200.s0141_linked_list_cycle;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
/*
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
public class Solution {
17+
public boolean hasCycle(ListNode head) {
18+
if (head == null) {
19+
return false;
20+
}
21+
ListNode fast = head.next;
22+
ListNode slow = head;
23+
while (fast != null && fast.next != null) {
24+
if (fast == slow) {
25+
return true;
26+
}
27+
fast = fast.next.next;
28+
slow = slow.next;
29+
}
30+
return false;
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0101_0200.s0142_linked_list_cycle_ii;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
/*
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
public class Solution {
17+
public ListNode detectCycle(ListNode head) {
18+
if (head == null || head.next == null) {
19+
return null;
20+
}
21+
ListNode slow = head;
22+
ListNode fast = head;
23+
while (fast != null && fast.next != null) {
24+
fast = fast.next.next;
25+
slow = slow.next;
26+
// intersected inside the loop.
27+
if (slow == fast) {
28+
break;
29+
}
30+
}
31+
if (fast == null || fast.next == null) {
32+
return null;
33+
}
34+
slow = head;
35+
while (slow != fast) {
36+
slow = slow.next;
37+
fast = fast.next;
38+
}
39+
return slow;
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0101_0200.s0143_reorder_list;
2+
3+
import com_github_leetcode.ListNode;
4+
5+
/*
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
public class Solution {
17+
ListNode forward;
18+
19+
public void reorderList(ListNode head) {
20+
forward = head;
21+
dfs(head);
22+
}
23+
24+
private void dfs(ListNode node) {
25+
if (node != null) {
26+
dfs(node.next);
27+
if (forward != null) {
28+
ListNode next = forward.next;
29+
// even case: forward.next coincide with node
30+
if (next == node) {
31+
node.next = null;
32+
} else {
33+
node.next = next;
34+
}
35+
forward.next = node;
36+
forward = node.next;
37+
}
38+
// odd case: forward coincide with node
39+
if (forward == node) {
40+
forward.next = null;
41+
forward = forward.next;
42+
}
43+
}
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0101_0200.s0141_linked_list_cycle;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void hasCycle() {
12+
ListNode listNode1 = new ListNode(3);
13+
listNode1.next = new ListNode(2);
14+
listNode1.next.next = new ListNode(0);
15+
listNode1.next.next.next = new ListNode(-4);
16+
listNode1.next.next.next.next = listNode1.next;
17+
assertThat(new Solution().hasCycle(listNode1), equalTo(true));
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0101_0200.s0142_linked_list_cycle_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void detectCycle() {
12+
ListNode listNode1 = new ListNode(3);
13+
listNode1.next = new ListNode(2);
14+
listNode1.next.next = new ListNode(0);
15+
listNode1.next.next.next = new ListNode(-4);
16+
listNode1.next.next.next.next = listNode1.next;
17+
assertThat(new Solution().detectCycle(listNode1), equalTo(listNode1.next));
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0101_0200.s0143_reorder_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.ListNode;
7+
import org.junit.Test;
8+
9+
public class SolutionTest {
10+
@Test
11+
public void reorderList() {
12+
ListNode listNode1 = new ListNode(1);
13+
listNode1.next = new ListNode(2);
14+
listNode1.next.next = new ListNode(3);
15+
listNode1.next.next.next = new ListNode(4);
16+
new Solution().reorderList(listNode1);
17+
assertThat(listNode1.toString(), equalTo("1, 4, 2, 3"));
18+
}
19+
}

0 commit comments

Comments
 (0)