@@ -101,16 +101,13 @@ class Solution:
101
101
# self.next = next
102
102
class Solution :
103
103
def removeNodes (self , head : Optional[ListNode]) -> Optional[ListNode]:
104
- dummy = ListNode(next = head)
104
+ dummy = ListNode(inf, head)
105
105
cur = head
106
- stk = []
106
+ stk = [dummy ]
107
107
while cur:
108
- while stk and stk [- 1 ].val < cur.val:
108
+ while stk[- 1 ].val < cur.val:
109
109
stk.pop()
110
- if stk:
111
- stk[- 1 ].next = cur
112
- else :
113
- dummy.next = cur
110
+ stk[- 1 ].next = cur
114
111
stk.append(cur)
115
112
cur = cur.next
116
113
return dummy.next
@@ -169,17 +166,14 @@ class Solution {
169
166
*/
170
167
class Solution {
171
168
public ListNode removeNodes (ListNode head ) {
172
- ListNode dummy = new ListNode (0 , head);
169
+ ListNode dummy = new ListNode (1 << 30 , head);
173
170
Deque<ListNode > stk = new ArrayDeque<> ();
171
+ stk. offerLast(dummy);
174
172
for (ListNode cur = head; cur != null ; cur = cur. next) {
175
- while (! stk . isEmpty() && stk. peekLast(). val < cur. val) {
173
+ while (stk. peekLast(). val < cur. val) {
176
174
stk. pollLast();
177
175
}
178
- if (! stk. isEmpty()) {
179
- stk. peekLast(). next = cur;
180
- } else {
181
- dummy. next = cur;
182
- }
176
+ stk. peekLast(). next = cur;
183
177
stk. offerLast(cur);
184
178
}
185
179
return dummy. next;
@@ -240,18 +234,14 @@ public:
240
234
class Solution {
241
235
public:
242
236
ListNode* removeNodes(ListNode* head) {
243
- ListNode* dummy = new ListNode(0 , head);
237
+ ListNode* dummy = new ListNode(1e9 , head);
244
238
ListNode* cur = head;
245
- vector<ListNode*> stk;
239
+ vector<ListNode*> stk = {dummy} ;
246
240
for (ListNode* cur = head; cur; cur = cur->next) {
247
- while (stk.size() && stk. back()->val < cur->val) {
241
+ while (stk.back()->val < cur->val) {
248
242
stk.pop_back();
249
243
}
250
- if (stk.size()) {
251
- stk.back()->next = cur;
252
- } else {
253
- dummy->next = cur;
254
- }
244
+ stk.back()->next = cur;
255
245
stk.push_back(cur);
256
246
}
257
247
return dummy->next;
@@ -301,17 +291,13 @@ func removeNodes(head *ListNode) *ListNode {
301
291
* }
302
292
*/
303
293
func removeNodes (head *ListNode ) *ListNode {
304
- dummy := &ListNode{Next: head}
305
- stk := []*ListNode{}
294
+ dummy := &ListNode{1 << 30 , head}
295
+ stk := []*ListNode{dummy }
306
296
for cur := head; cur != nil ; cur = cur.Next {
307
- for len (stk) > 0 && stk[len (stk)-1 ].Val < cur.Val {
297
+ for stk[len (stk)-1 ].Val < cur.Val {
308
298
stk = stk[:len (stk)-1 ]
309
299
}
310
- if len (stk) > 0 {
311
- stk[len (stk)-1 ].Next = cur
312
- } else {
313
- dummy.Next = cur
314
- }
300
+ stk[len (stk)-1 ].Next = cur
315
301
stk = append (stk, cur)
316
302
}
317
303
return dummy.Next
@@ -369,17 +355,13 @@ function removeNodes(head: ListNode | null): ListNode | null {
369
355
*/
370
356
371
357
function removeNodes(head : ListNode | null ): ListNode | null {
372
- const dummy = new ListNode (0 , head );
373
- const stk: ListNode [] = [];
358
+ const dummy = new ListNode (Infinity , head );
359
+ const stk: ListNode [] = [dummy ];
374
360
for (let cur = head ; cur ; cur = cur .next ) {
375
- while (stk .length && stk . at (- 1 )! .val < cur .val ) {
361
+ while (stk .at (- 1 )! .val < cur .val ) {
376
362
stk .pop ();
377
363
}
378
- if (stk .length ) {
379
- stk .at (- 1 )! .next = cur ;
380
- } else {
381
- dummy .next = cur ;
382
- }
364
+ stk .at (- 1 )! .next = cur ;
383
365
stk .push (cur );
384
366
}
385
367
return dummy .next ;
0 commit comments