-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0083.py
More file actions
41 lines (33 loc) · 1.13 KB
/
0083.py
File metadata and controls
41 lines (33 loc) · 1.13 KB
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
# 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
# 示例 1:
# 输入: 1->1->2
# 输出: 1->2
# 示例 2:
# 输入: 1->1->2->3->3
# 输出: 1->2->3
# 来源:力扣(LeetCode)
# 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
# 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
cursorNode = head
while cursorNode != None:
if cursorNode.next != None and cursorNode.val == cursorNode.next.val:
cursorNode.next = cursorNode.next.next
else:
cursorNode = cursorNode.next
return head
so = Solution()
head = ListNode(1)
head.next = ListNode(1)
head.next.next = ListNode(1)
# head.next.next = ListNode(2)
# head.next.next.next = ListNode(3)
# head.next.next.next.next = ListNode(3)
o = so.deleteDuplicates(head)
o