/
83-deleteDuplicate.py
81 lines (62 loc) · 1.51 KB
/
83-deleteDuplicate.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: zenithude
"""
class ListNode(object):
"""List of Nodes Double Linked."""
def __init__(self, x):
"""
Initialize the list.
Parameters
----------
x : TYPE int
Returns
-------
None.
"""
self.val = x
self.next = None
self.prev = None
def constructListNode(arr):
"""
Construct ListNode with a List
:param arr: List()
:return: ListNode()
"""
NewListNode = ListNode(arr[0])
arr.pop(0)
if len(arr) > 0:
NewListNode.next = constructListNode(arr)
return NewListNode
def headLength(head):
"""
Function return number of Nodes in ListNode().
:param head: ListNode()
:return: int
"""
numberNode = 0
while head is not None:
head = head.next
numberNode += 1
return numberNode
def listPrint(node):
print("None", end='<-->')
while node is not None:
print(node.val, end='<-->')
last = node
node = node.next
print(node)
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if not head:
return None
now = head
cur = head
while now.next:
cur = now.next
if cur.val == now.val:
now.next = cur.next
else:
now = now.next
return head