Skip to content

Commit f1edac8

Browse files
Problem 876 (#20)
* add solution and resources * update readme
1 parent 16d8014 commit f1edac8

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
2727
19. [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
2828
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
2929
21. [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
30-
22. Middle of the Linked List #876
30+
22. [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
3131
23. Maximum Depth of Binary Tree #104
3232
24. Contains Duplicate #217
3333

@@ -120,6 +120,7 @@ In order to practice with similar data structures I'll be placing each problem i
120120
- [Convert Binary Number in a Linked List to Integer #1290](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/binary-linked-list-1290.md)
121121
- [Linked List Cycle #141](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/linked-list-cycle-141.md)
122122
- [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
123+
- [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
123124

124125
### Hash Table
125126

@@ -149,6 +150,7 @@ Within the problems above there are several patterns that often occur. I plan to
149150
- [Valid Palindrome #125](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/valid-palindrome-125.md)
150151
- [Linked List Cycle #141](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/linked-list-cycle-141.md)
151152
- [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
153+
- [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
152154

153155
### Binary Search
154156

easy/middle-linked-list-876.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Middle of the Linked List
2+
3+
Page on leetcode:https://leetcode.com/problems/middle-of-the-linked-list/
4+
5+
## Problem Statement
6+
7+
Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node.
8+
9+
### Constraints
10+
11+
- The number of nodes in the list is in the range [1, 100].
12+
- 1 <= Node.val <= 100
13+
14+
### Example
15+
16+
```
17+
Input: head = [1,2,3,4,5]
18+
Output: [3,4,5]
19+
Explanation: The middle node of the list is node 3.
20+
```
21+
22+
## Solution
23+
24+
### Pseudocode
25+
26+
1. Find the length of the linked list.
27+
2. Find the middle floor(length/2) + 1
28+
3. Run a second loop and update head to middle
29+
4. Return head
30+
31+
### Initial Solution
32+
33+
This solution has a time complexity of O(n) as we have to touch every node and a space complexity of O(1) as we only stored the mid point.
34+
35+
```javascript
36+
const middleNode = function (head) {
37+
let newHead = head;
38+
let length = 0;
39+
while (newHead) {
40+
length++;
41+
newHead = newHead.next;
42+
}
43+
44+
let midpoint = Math.floor(length / 2) + 1;
45+
46+
while (midpoint > 1) {
47+
head = head.next;
48+
midpoint--;
49+
}
50+
51+
return head;
52+
};
53+
```
54+
55+
### Optimized Solution
56+
57+
While the below solution has the same time and space complexity of above it is more efficient. You can see and explanation here: https://www.youtube.com/watch?v=wmpivqMlClI
58+
59+
```javascript
60+
const middleNode = function (head) {
61+
let slow = head;
62+
let fast = head;
63+
64+
while (fast && fast.next) {
65+
slow = slow.next;
66+
fast = fast.next.next;
67+
}
68+
69+
return slow;
70+
};
71+
```

0 commit comments

Comments
 (0)