Skip to content

Commit 65baf54

Browse files
add 876
1 parent 0881535 commit 65baf54

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Your ideas/fixes/algorithms are more than welcome!
3838
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
3939
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java) | O(n) | O(1) | |Easy|
4040
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java) | O(n) | O(k) | |Easy|
41+
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_876.java) | O(n) | O(1) | |Easy|
4142
|867|[Transpose Matrix](https://leetcode.com/problems/transpose-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_867.java) | O(r*c) | O(r*c) | |Easy|
4243
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_852.java) | O(n) | O(1) | |Easy|
4344
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | O(n) | O(1) | |Easy|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
5+
/**
6+
* 876. Middle of the Linked List
7+
*
8+
* Given a non-empty, singly linked list with head node head, return a middle node of linked list.
9+
*
10+
* If there are two middle nodes, return the second middle node.
11+
*
12+
* Example 1:
13+
*
14+
* Input: [1,2,3,4,5]
15+
* Output: Node 3 from this list (Serialization: [3,4,5])
16+
* The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
17+
* Note that we returned a ListNode object ans, such that:
18+
* ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
19+
*
20+
* Example 2:
21+
*
22+
* Input: [1,2,3,4,5,6]
23+
* Output: Node 4 from this list (Serialization: [4,5,6])
24+
* Since the list has two middle nodes with values 3 and 4, we return the second one.
25+
*
26+
* Note:
27+
*
28+
* The number of nodes in the given list will be between 1 and 100.
29+
*/
30+
public class _876 {
31+
public static class Solution1 {
32+
public ListNode middleNode(ListNode head) {
33+
ListNode fast = head;
34+
ListNode slow = head;
35+
while (fast != null && fast.next != null) {
36+
fast = fast.next.next;
37+
slow = slow.next;
38+
}
39+
return slow;
40+
}
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.ListNode;
4+
import com.fishercoder.solutions._325;
5+
import com.fishercoder.solutions._876;
6+
import java.util.Arrays;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _876Test {
13+
private static _876.Solution1 solution1;
14+
private static ListNode head;
15+
private static ListNode middle;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _876.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5));
25+
middle = solution1.middleNode(head);
26+
assertEquals(middle, ListNode.createSinglyLinkedList(Arrays.asList(3, 4, 5)));
27+
}
28+
29+
@Test
30+
public void test2() {
31+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4, 5, 6));
32+
middle = solution1.middleNode(head);
33+
assertEquals(middle, ListNode.createSinglyLinkedList(Arrays.asList(4, 5, 6)));
34+
}
35+
}

0 commit comments

Comments
 (0)