python数据结构之单链表的逆序
题目描述
给定带头结点的单链表,将其逆序
分析
由于单链表与数组的不同,单链表的每个结点的地址都储存在其前驱结点的指针域中,对单链表中任一结点的访问只能从头结点开始遍历。在对链表操作的时候应该注意在修改指针域的时候保存其后继结点的地址,以免丢失后继结点
方法一、就地逆序
方法介绍
在遍历链表的时候,修改当前节点指针域的指向,使其指向他的前驱节点,这就需要三个指针变量保存结点指针域:当前结点、前驱结点和后继结点
源代码
class Node(object):
"""结点"""
def __init__(self,data=None):
self.data = data
self.next = None
def reverse(head):
"""链表逆序"""
if head == None or head.next == None:
return
cur = head.next #当前节点
next = cur.next #后继节点
#将原来的头结点变为为节点
pre = cur #前驱节点
cur.next = None
cur = next
while cur.next != None:
next = cur.next
cur.next = pre
pre = cur
cur = next
#最后一个进不了循环,在外面整
cur.next = pre
head.next = cur
def main():
#创建单链表
print("逆序前的链表为:", end=" ")
head = Node()
for i in range(10):
"""创建单链表"""
node = Node(i)
cur = head
if head.next == None or head == None:
head.next = node
else:
while cur.next != None:
cur = cur.next
cur.next = node
cur = head
while cur.next != None:
cur = cur.next
print(cur.data, end=" ")
print("\n逆序后的链表为:", end=" ")
reverse(head)
cur = head
while cur.next != None:
cur = cur.next
print(cur.data, end=" ")
if __name__ == '__main__':
main()
算法性能分析
以上方法只需要对链表进行一次遍历,因此时间复杂度为O(N),N为链表的长度。此种方法需要两个额外的变量储存前驱结点和后继结点,所以它的空间复杂度为O(1)。
方法二、插入法逆序
方法介绍
从链表的第二个结点开始,把遍历到的结点插入到头结点的后面,直到遍历结束。
源代码
class Node(object):
"""结点"""
def __init__(self,data=None):
self.data = data
self.next = None
def reverse(head):
"""链表逆序"""
if head == None or head.next == None:
return
cur = head.next.next
head.next.next = None
while cur != None:
next = cur.next
cur.next = head.next
head.next = cur
cur = next
def main():
#创建单链表
print("逆序前的链表为:", end=" ")
head = Node()
for i in range(10):
"""创建单链表"""
node = Node(i)
cur = head
if head.next == None or head == None:
head.next = node
else:
while cur.next != None:
cur = cur.next
cur.next = node
cur = head
while cur.next != None:
cur = cur.next
print(cur.data, end=" ")
print("\n逆序后的链表为:", end=" ")
reverse(head)
cur = head
while cur.next != None:
cur = cur.next
print(cur.data, end=" ")
if __name__ == '__main__':
main()
算法性能分析
这种方法也只需要对链表进行一次遍历,时间复杂度同样为O(n),与方法一相比,这种方法不需要额外变量保存前驱结点地址,因此空间复杂度更低