Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4f7a148

Browse files
committedJan 10, 2022
Add solution #2095
1 parent 7fea737 commit 4f7a148

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
2011|[Final Value of Variable After Performing Operations](./2011-final-value-of-variable-after-performing-operations.js)|Easy|
238238
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
239239
2047|[Number of Valid Words in a Sentence](./2047-number-of-valid-words-in-a-sentence.js)|Easy|
240+
2095|[Delete the Middle Node of a Linked List](./2095-delete-the-middle-node-of-a-linked-list.js)|Medium|
240241
2114|[Maximum Number of Words Found in Sentences](./2114-maximum-number-of-words-found-in-sentences.js)|Easy|
241242

242243
## License
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 2095. Delete the Middle Node of a Linked List
3+
* https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/
4+
* Difficulty: Medium
5+
*
6+
* You are given the head of a linked list. Delete the middle node, and return the head
7+
* of the modified linked list.
8+
*
9+
* The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using
10+
* 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
11+
*
12+
* For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
13+
*/
14+
15+
/**
16+
* Definition for singly-linked list.
17+
* function ListNode(val, next) {
18+
* this.val = (val===undefined ? 0 : val)
19+
* this.next = (next===undefined ? null : next)
20+
* }
21+
*/
22+
/**
23+
* @param {ListNode} head
24+
* @return {ListNode}
25+
*/
26+
var deleteMiddle = function(head) {
27+
let slow = new ListNode(0, head);
28+
let fast = head;
29+
30+
if (!head.next) {
31+
return null;
32+
}
33+
34+
while (fast && fast.next) {
35+
slow = slow.next;
36+
fast = fast.next.next;
37+
}
38+
39+
slow.next = slow.next.next;
40+
41+
return head;
42+
};

0 commit comments

Comments
 (0)
Please sign in to comment.