Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea0ec4b

Browse files
feat: add typescript solution to lc problem: No.2095
No.2095.Delete the Middle Node of a Linked List
1 parent 4970aed commit ea0ec4b

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

‎solution/2000-2099/2095.Delete the Middle Node of a Linked List/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,28 @@ class Solution {
123123
<!-- 这里可写当前语言的特殊实现逻辑 -->
124124

125125
```ts
126+
/**
127+
* Definition for singly-linked list.
128+
* class ListNode {
129+
* val: number
130+
* next: ListNode | null
131+
* constructor(val?: number, next?: ListNode | null) {
132+
* this.val = (val===undefined ? 0 : val)
133+
* this.next = (next===undefined ? null : next)
134+
* }
135+
* }
136+
*/
126137

138+
function deleteMiddle(head: ListNode | null): ListNode | null {
139+
if (!head || !head.next) return null;
140+
let fast = head.next, slow = head;
141+
while (fast.next && fast.next.next) {
142+
slow = slow.next;
143+
fast = fast.next.next;
144+
}
145+
slow.next = slow.next.next;
146+
return head;
147+
};
127148
```
128149

129150
### **...**

‎solution/2000-2099/2095.Delete the Middle Node of a Linked List/README_EN.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,28 @@ class Solution {
106106
### **TypeScript**
107107

108108
```ts
109+
/**
110+
* Definition for singly-linked list.
111+
* class ListNode {
112+
* val: number
113+
* next: ListNode | null
114+
* constructor(val?: number, next?: ListNode | null) {
115+
* this.val = (val===undefined ? 0 : val)
116+
* this.next = (next===undefined ? null : next)
117+
* }
118+
* }
119+
*/
109120

121+
function deleteMiddle(head: ListNode | null): ListNode | null {
122+
if (!head || !head.next) return null;
123+
let fast = head.next, slow = head;
124+
while (fast.next && fast.next.next) {
125+
slow = slow.next;
126+
fast = fast.next.next;
127+
}
128+
slow.next = slow.next.next;
129+
return head;
130+
};
110131
```
111132

112133
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function deleteMiddle(head: ListNode | null): ListNode | null {
14+
if (!head || !head.next) return null;
15+
let fast = head.next, slow = head;
16+
while (fast.next && fast.next.next) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
}
20+
slow.next = slow.next.next;
21+
return head;
22+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /