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 5469431

Browse files
authored
Create Remove Duplicates from Sorted List II.py
1 parent 6a8e3fa commit 5469431

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
2+
class Solution:
3+
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
4+
dummy = ListNode(0)
5+
prev = dummy
6+
dummy.next = head
7+
8+
while head and head.next:
9+
if head.val == head.next.val:
10+
while head and head.next and head.val == head.next.val:
11+
head.next = head.next.next
12+
head = head.next
13+
prev.next = head
14+
else:
15+
head = head.next
16+
prev = prev.next
17+
18+
return dummy.next

0 commit comments

Comments
(0)

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