python去除链表重复项
class Node:
def init(self,data=None):
self.data=data
self.next=None
class ListNode:
def init(self):
self.head=Node()
def append(self,num):
node=Node(num)
cur=self.head
while cur.next!=None:
cur=cur.next
cur.next=node
@property
def lastNode(self):
cur=self.head
while cur.next!=None:
cur=cur.next
return cur
def print(self):
cur=self.head
while cur.next!=None:
print('data:',cur.data)
cur=cur.next
print('end:',cur.data)
def remove(self):
head=self.head
cur=head.next.next
pre=head.next
while cur!=None:
if pre.data==cur.data:
pre.next=cur.next
cur=cur.next
else:
pre=cur
cur=cur.next
if name == 'main':
list=ListNode()
list.append(1)
list.append(2)
list.append(2)
list.append(3)
list.print()
list.remove()
print('移除重复项')
list.print()
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持一下吧