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 9ac50a9

Browse files
chore: Add new solution to LeetCode 203
Without using pre Node
1 parent 7c752af commit 9ac50a9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎problems/0203.移除链表元素.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,27 @@ public ListNode removeElements(ListNode head, int val) {
234234
}
235235
return head;
236236
}
237+
/**
238+
* 不添加虚拟节点and pre Node方式
239+
* 时间复杂度 O(n)
240+
* 空间复杂度 O(1)
241+
* @param head
242+
* @param val
243+
* @return
244+
*/
245+
public ListNode removeElements(ListNode head, int val) {
246+
while(head!=null && head.val==val){
247+
head = head.next;
248+
}
249+
ListNode curr = head;
250+
while(curr!=null){
251+
while(curr.next!=null && curr.next.val == val){
252+
curr.next = curr.next.next;
253+
}
254+
curr = curr.next;
255+
}
256+
return head;
257+
}
237258
```
238259

239260
Python:

0 commit comments

Comments
(0)

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