-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path82.删除排序链表中的重复元素-ii.py
128 lines (108 loc) · 2.96 KB
/
82.删除排序链表中的重复元素-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# @lc app=leetcode.cn id=82 lang=python3
#
# [82] 删除排序链表中的重复元素 II
#
# https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/description/
#
# algorithms
# Medium (44.59%)
# Likes: 221
# Dislikes: 0
# Total Accepted: 33.6K
# Total Submissions: 72.8K
# Testcase Example: '[1,2,3,3,4,4,5]'
#
# 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
#
# 示例 1:
#
# 输入: 1->2->3->3->4->4->5
# 输出: 1->2->5
#
#
# 示例 2:
#
# 输入: 1->1->1->2->3
# 输出: 2->3
#
#
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def __str__(self):
s = f'{self.val}'
tmp = self.next
while tmp is not None:
s += f'-->{tmp.val}'
tmp = tmp.next
return s + '-->NULL'
def __repr__(self):
return self.__str__()
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteDuplicates_1(self, head: ListNode) -> ListNode:
"""
1. 递归
"""
if not head or not head.next:
return head
if head.val != head.next.val:
p = self.deleteDuplicates(head.next)
head.next = p
return head
else:
while head.next and head.val == head.next.val:
head = head.next
return self.deleteDuplicates(head.next)
def deleteDuplicates(self, head: ListNode) -> ListNode:
"""
TODO: 2. 迭代,快慢指针
"""
if not head or not head.next:
return head
dummy = ListNode(None)
dummy.next = head
slow = dummy
fast = dummy.next
while fast:
if fast.next and fast.next.val == fast.val:
tmp = fast.val
while fast and tmp == fast.val:
fast = fast.next
else:
slow.next = fast
slow = fast
fast = fast.next
slow.next = fast
return dummy.next
def deleteDuplicates_2(self, head: ListNode) -> ListNode:
"""
TODO: 2. 迭代
"""
dummy = ListNode(0)
dummy.next = head
prev = dummy
curr = head
flag = False # 表征是否出现了重复的元素
while curr:
while curr.next and curr.next.val == curr.val:
flag = True
curr = curr.next
# 移动到相同元素的最后一位
if flag and curr:
# 出现了重复元素时,继续下移一位 --> 忽略了所有相同元素
prev.next = curr.next
flag = False
else:
prev.next = curr
prev = curr
curr = curr.next
return dummy.next
# @lc code=end