给定一个链表,将该链表翻转,并返回头结点
#链表的翻转
class Node:
def __init__(self,val=None):
self.val = val
self.next = None
class LinkList:
def __init__(self):
self.head = Node()
def fanzhuan1(self):
phead = self.head #头结点
if not phead or not phead.next:
return phead
last = None #上一个节点
while phead:
tmp = phead.next #用一个变量将下一个节点暂存起来
phead.next = last #将节点翻转
last = phead #让last,pHead依次向后移动一个节点,继续下一次的指针反转
phead = tmp
return last