-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path删除链表中重复的结点(不包括重复结点).py
88 lines (64 loc) · 1.99 KB
/
删除链表中重复的结点(不包括重复结点).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
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplication(self, pHead):
# write code here
if pHead == None or pHead.next == None:
return pHead
new_pHead = ListNode('-1')
new_pHead.next = pHead
pre = new_pHead
p = pHead
p_next = None
while p != None and p.next != None:
p_next = p.next
if p.val == p_next.val:
while p_next != None and p.val == p_next.val:
p_next = p_next.next
pre.next = p_next
p = p_next
else:
pre = p
p = p.next
return new_pHead.next
lian_biao_first = ListNode('1')
lian_biao_first.next = ListNode('2')
lian_biao_second = lian_biao_first.next
lian_biao_second.next = ListNode('2')
lian_biao_third = lian_biao_second.next
lian_biao_third.next = ListNode('4')
lian_biao_fourth = lian_biao_third.next
lian_biao_fourth.next = ListNode('5')
lian_biao_fifth = lian_biao_fourth.next
lian_biao_fifth.next = ListNode('5')
lian_biao_sixth = lian_biao_fifth.next
lian_biao_sixth.next = ListNode('5')
lian_biao_seventh = lian_biao_sixth.next
lian_biao_seventh.next = ListNode('6')
lian_biao_eigth = lian_biao_seventh.next
tmp = lian_biao_first
while tmp:
print tmp.val
tmp = tmp.next
print '-----------------------\n'
new_pHead = ListNode('-1')
new_pHead.next = lian_biao_first
tmp_new = new_pHead
tmp = lian_biao_first
while tmp != None and tmp.next != None:
tmp_next = tmp.next
if tmp.val == tmp_next.val:
while tmp_next != None and tmp.val == tmp_next.val:
tmp_next = tmp_next.next
tmp_new.next = tmp_next
tmp = tmp_next
else:
tmp_new = tmp
tmp = tmp.next
test_tmp = new_pHead.next
while test_tmp:
print test_tmp.val
test_tmp = test_tmp.next