Python解决链表反转
思路:从head节点开始遍历,依次将遍历到的节点插入head之后
插入法
// 定义Node
class Node(object):
def __init__(self, value, next = None):
self.val = value
self.next = next
def reserse(self):
if head is None or head.next is None:
return
cur = None
next = None
cur.next = head.next.next
head.next.next = None
while cur is not None:
next = cur.next
cur.next = had.next
head.next = cur
cur = next