Skip to content

Problem 876 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
19. [Majority Element #169](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/majority-element-169.md)
20. [Add Binary #67](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/add-binary-67.md)
21. [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
22. Middle of the Linked List #876
22. [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)
23. Maximum Depth of Binary Tree #104
24. Contains Duplicate #217

Expand Down Expand Up @@ -120,6 +120,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [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)
- [Linked List Cycle #141](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/linked-list-cycle-141.md)
- [Reverse Linked List #206](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/reverse-linked-list-206.md)
- [Middle of the Linked List #876](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/middle-linked-list-876.md)

### Hash Table

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

### Binary Search

Expand Down
71 changes: 71 additions & 0 deletions easy/middle-linked-list-876.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Middle of the Linked List

Page on leetcode:https://leetcode.com/problems/middle-of-the-linked-list/

## Problem Statement

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.

### Constraints

- The number of nodes in the list is in the range [1, 100].
- 1 <= Node.val <= 100

### Example

```
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.
```

## Solution

### Pseudocode

1. Find the length of the linked list.
2. Find the middle floor(length/2) + 1
3. Run a second loop and update head to middle
4. Return head

### Initial Solution

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.

```javascript
const middleNode = function (head) {
let newHead = head;
let length = 0;
while (newHead) {
length++;
newHead = newHead.next;
}

let midpoint = Math.floor(length / 2) + 1;

while (midpoint > 1) {
head = head.next;
midpoint--;
}

return head;
};
```

### Optimized Solution

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

```javascript
const middleNode = function (head) {
let slow = head;
let fast = head;

while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}

return slow;
};
```