题意:
在链表类中实现这些功能:
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
这是一道练习链表基础比较好的题目。
# 定义链表
class ListNode(object):def __init__(self, val=0, next=None):self.val = valself.next = nextclass MyLinkedList(object):def __init__(self): # 下方可以直接赋值 里面不用参数也可self.dummy_node = ListNode()self.size = 0 # 注意定义一个 sizedef get(self, index):""":type index: int:rtype: int"""if index < 0 or index >= self.size:return -1current = self.dummy_node.nextfor i in range(index):current = current.nextreturn current.valdef addAtHead(self, val):""":type val: int:rtype: None"""temp = ListNode()temp.val = valtemp.next = self.dummy_node.nextself.dummy_node.next = tempself.size += 1 # 注意 size+1def addAtTail(self, val):""":type val: int:rtype: None"""temp = ListNode()temp.val = valcurrent = self.dummy_nodefor i in range(self.size):current = current.nextcurrent.next = temp# temp.next = None 因为节点定义里面已经有了self.size += 1def addAtIndex(self, index, val):""":type index: int:type val: int:rtype: None"""if index < 0 or index > self.size: # 注意这里没有等于 因为下面是range(index)也不会取到returntemp = ListNode()temp.val = valcurrent = self.dummy_nodefor i in range(index):current = current.nexttemp.next = current.nextcurrent.next = tempself.size += 1 # 注意加1def deleteAtIndex(self, index):""":type index: int:rtype: None"""if index < 0 or index >= self.size:returncurrent = self.dummy_nodefor i in range(index):current = current.next # 先到达位置current.next = current.next.next # 再执行删除self.size -= 1# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
难点或者容易出错的地方在于:一些边界的处理 以及 代码健壮性处理 的地方 :
# 双链表
class ListNode(object):def __init__(self, val=0, pred=None, next=None):self.val = valself.pred = predself.next = nextclass MyLinkedList(object):def __init__(self):self.head = Noneself.tail = Noneself.size = 0def get(self, index):if index < 0 or index >= self.size:return -1if index < self.size // 2: # index在前面一半 就用头节点来遍历 current = self.headfor i in range(index):current = current.nextelse:current = self.tail# x = 0# while x < self.size - index - 1:# current = current.pred# x += 1for i in range(self.size - index - 1):current = current.predreturn current.valdef addAtHead(self, val):temp = ListNode(val=val, next=self.head) # 记得用selfif self.head:self.head.pred = tempelse: # 需要判断是否存在头节点self.tail = temp # 不存在 那么新节点赋给头尾节点self.head = tempself.size += 1def addAtTail(self, val):temp = ListNode(val=val, pred=self.tail)if self.tail: # 需要判断尾节点是否存在self.tail.next = tempelse:self.tail = tempself.head = tempself.size += 1def addAtIndex(self, index, val):if index < 0 or index > self.size:returnif index == 0:self.addAtHead(val)elif index == self.size:self.addAtTail(val)else:if index < self.size // 2: # 在前面一半current = headfor i in range(index-1):current = current.nextelse:current = self.tailx = 0while x < self.size - index:current = current.predx += 1temp = ListNode(val=val)current.next.pred = tempcurrent.next = tempself.size += 1def deleteAtIndex(self, index):if index < 0 or index >= self.size:returnif index == 0:self.head = self.head.next # 下一个节点是头节点if self.head: # 还需要判断是否存在self.head.pred = Noneelse:self.tail = None # 不存在完全就是空的elif index == self.size - 1: # !!! 很多细节self.tail = self.tail.previf self.tail:self.tail.next = Noneelse:self.head = Noneelse:if index < self.size // 2:current = self.headfor i in range(index):current = current.nextelse:current = self.tailx = 0while x < self.size - index - 1:current = current.predx += 1current.pred.next = current.nextcurrent.next.pred = current.predself.size -= 1 # 记得减1
参考:https://programmercarl.com/