File tree 3 files changed +64
-0
lines changed
solution/2000-2099/2095.Delete the Middle Node of a Linked List
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -123,7 +123,28 @@ class Solution {
123
123
<!-- 这里可写当前语言的特殊实现逻辑 -->
124
124
125
125
``` 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
+ */
126
137
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
+ };
127
148
```
128
149
129
150
### ** ...**
Original file line number Diff line number Diff line change @@ -106,7 +106,28 @@ class Solution {
106
106
### ** TypeScript**
107
107
108
108
``` 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
+ */
109
120
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
+ };
110
131
```
111
132
112
133
### ** ...**
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments