1.单链表和双链表的反转
class Link():
# 单链表和双链表的反转
def node(self, value):
self.value = value
self.next = None
def doublenode(self, value):
self.value = value
self.last = None
self.next = None
def nodereverse(self, head):
pre = None
next = None
while head != None:
next = head.next
head.next = pre
pre = head
head = next
return pre
def doublenodereverse(self, head):
pre = None
next = None
while head != None:
next = head.next
head.next = pre
head.last = next
pre = head
head = next
return pre
2.把给定的值都删除
class Linkremove():
def node(self, value):
self.value = value
self.next = None
def linkremove(self, head, num):
while head != None:
if head.value != num:
break
head = head.next
# head 来到第一个不需要删除的位置
pre = head
cur = head
while cur != None:
if cur.value == num:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return head