Skip to content

Commit 9d50c37

Browse files
solves middle of the linkedList
1 parent 769c90d commit 9d50c37

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap) | [![Java](assets/java.png)](src/BinaryGap.java) |
237237
| 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees) | [![Java](assets/java.png)](src/LeafSimilarTrees.java) |
238238
| 874 | [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation) | |
239-
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | |
239+
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [![Java](assets/java.png)](src/MiddleOfTheLinkedList.java) |
240240
| 883 | [Projection Area of 3D Shapes](https://leetcode.com/problems/projection-area-of-3d-shapes) | |
241241
| 884 | [Uncommon Words from 2 Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences) | |
242242
| 888 | [Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap) | |

src/MiddleOfTheLinkedList.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class MiddleOfTheLinkedList {
2+
public static class ListNode {
3+
int val;
4+
ListNode next;
5+
ListNode() {}
6+
ListNode(int val) { this.val = val; }
7+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
8+
}
9+
10+
public ListNode middleNode(ListNode head) {
11+
ListNode slow = head, fast = head;
12+
while (fast != null && fast.next != null) {
13+
slow = slow.next;
14+
fast = fast.next.next;
15+
}
16+
return slow;
17+
}
18+
}

0 commit comments

Comments
 (0)