1. 链表结构
与数组相似,链表也是一种线性数据结构。
链表有两种类型:单链表和双链表
本文只讲述单链表。
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
2. 链表创建
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class ListLink(object):
def __init__(self, head=None):
self.head = head
def initlink(self, ListNums):
if not ListNums:
return
head = ListNode(0)
prehead = head
for num in ListNums:
head.next = ListNode(num)
head = head.next
self.head = prehead.next
def link2list(self):
ListNums = []
node = self.head
while node:
ListNums.append(node.val)
node = node.next
return ListNums
ListNums = [24, 33, 21, 1, 76, 45, 61]
link = ListLink()
link.initlink(ListNums)
print(link.link2list())
[24, 33, 21, 1, 76, 45, 61]
不在ListNode类中写类方法,是因为如果要删除只有一个节点的链表,就需要将链表对象置为None,对象无法调用成员函数将自身置为None(不知道怎么实现)。
3. 插入节点
class ListLink(object):
def __init__(self, head=None):
self.head = head
def insertNode(self, num, index="end"):
node = ListNode(num)
# 在表头插入
if index == 0:
self.head, node.next = node, self.head
return
# 在[1:len-1)范围插入
curnode = self.head
i = 1
while True:
if index == i:
curnode.next, node.next = node, curnode.next
return
i += 1
if not curnode.next:
break
curnode = curnode.next
# 在表尾插入
if index == "end":
curnode.next = node
ListNums = [24, 33, 21, 1, 76, 45, 61]
head = ListNode().list2link(ListNums)
# 表头插入
link.insertNode(10, 0)
print(link.link2list())
[10, 24, 33, 21, 1, 76, 45, 61]
# 表中插入
link.insertNode(20, 3)
print(link.link2list())
[10, 24, 33, 20, 21, 1, 76, 45, 61]
# 表尾插入
link.insertNode(30)
print(link.link2list())
link.insertNode(40, len(link.link2list()))
print(link.link2list())
link.insertNode(50, "end")
print(link.link2list())
[10, 24, 33, 20, 21, 1, 76, 45, 61, 30]
[10, 24, 33, 20, 21, 1, 76, 45, 61, 30, 40]
[10, 24, 33, 20, 21, 1, 76, 45, 61, 30, 40, 50]
4. 删除节点
class ListLink(object):
def __init__(self, head=None):
self.head = head
def deletNode(self, index="end"):
if not self.head:
return
# 删除表头
if index == 0:
self.head = self.head.next
return
# 删除[1:len-1]范围节点
if not self.head.next and index == "end":
self.head = None
return
i = 1
curnode = self.head
while curnode.next:
if index == i:
curnode.next = curnode.next.next
return
if not curnode.next.next:
break
i += 1
curnode = curnode.next
if index == "end":
curnode.next = None
print("超出范围")
ListNums = [24, 33, 21, 1, 76, 45, 61]
link = ListLink()
link.initlink(ListNums)
print(link.link2list())
link.deletNode(0)
print(link.link2list())
link.deletNode(4)
print(link.link2list())
link.deletNode()
print(link.link2list())
link.deletNode("end")
print(link.link2list())
link.deletNode(10)
print(link.link2list())
link.deletNode(2)
print(link.link2list())
link.deletNode(0)
print(link.link2list())
link.deletNode()
print(link.link2list())
[24, 33, 21, 1, 76, 45, 61]
[33, 21, 1, 76, 45, 61]
[33, 21, 1, 76, 61]
[33, 21, 1, 76]
[33, 21, 1]
超出范围
[33, 21, 1]
[33, 21]
[21]
[]
5. 查询节点
class ListLink(object):
def __init__(self, head=None):
self.head = head
def getNode(self, index):
curnode = self.head
while index and curnode:
curnode = curnode.next
index -= 1
if curnode:
return curnode.val
return curnode
ListNums = [24, 33, 21, 1, 76, 45, 61]
link = ListLink()
link.initlink(ListNums)
print(link.getNode(0))
print(link.getNode(5))
print(link.getNode(6))
print(link.getNode(7))
24
45
61
None
6. 查找节点
class ListLink(object):
def __init__(self, head=None):
self.head = head
def queryNode(self, val):
curnode = self.head
i = 0
while curnode:
if curnode.val == val:
return i
i += 1
curnode = curnode.next
print(link.queryNode(24))
print(link.queryNode(76))
print(link.queryNode(61))
print(link.queryNode(100))
0
4
6
None