本文目录
链表理论基础
看文章
- 链表类型
- 单链表
- 双链表
- 循环链表
- 链表的存储:散乱分布,通过指针链接。C、C++需要手动处理内存,JAVA、python可以自动处理内存。
- 链表的定义(Python):
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
- 链表操作:
- 删除
- 添加
- 性能分析(转载自代码随想录)
203.移除链表元素
代码随想录:203.移除链表元素
Leetcode:203.移除链表元素
直接做题
链表思路能理解,但思路一直没写好(非规范思路,处理边界很麻烦)。
看文章
时间复杂度: O(n)
空间复杂度: O(1)
直接使用原来的链表来进行删除操作
移除头结点:head = head.next,与移除其他节点的逻辑不一样。
设置一个虚拟头结点在进行删除操作
设置虚拟头结点,指向头结点,使移除头结点和移除其他节点的逻辑统一,最后return 虚拟头结点.next。
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
# 创建虚拟头部节点以简化删除过程
dummy_head = ListNode(next = head)
# 遍历列表并删除值为val的节点
current = dummy_head
while current.next:
if current.next.val == val:
current.next = current.next.next
else:
current = current.next
return dummy_head.next
707.设计链表
代码随想录:707.设计链表
Leetcode:707.设计链表
直接做题
没写过class,有点犯怵。
代码随想录解法
- 单链表实现:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.dummy_head = ListNode()
self.size = 0
def get(self, index: int) -> int:
if index < 0 or index >= self.size:
return -1
current = self.dummy_head.next
for i in range(index):
current = current.next
return current.val
def addAtHead(self, val: int) -> None:
self.dummy_head.next = ListNode(val, self.dummy_head.next)
self.size += 1
def addAtTail(self, val: int) -> None:
current = self.dummy_head
while current.next:
current = current.next
current.next = ListNode(val)
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index < 0 or index > self.size: # 这里的判别比较不同
return
current = self.dummy_head
for i in range(index):
current = current.next
current.next = ListNode(val, current.next)
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
current = self.dummy_head
for 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)
时间复杂度: 涉及 index 的相关操作为 O(index), 其余为 O(1)
空间复杂度: O(n)
看完觉得简单,实际上还是有点难度的,主要还是在class的写法和细节的处理,应该要多举例尝试。
- 用双链表实现:
class ListNode:
def __init__(self, val=0, prev=None, next=None):
self.val = val
self.prev = prev
self.next = next
class MyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def get(self, index: int) -> int:
if index < 0 or index >= self.size:
return -1
if index < self.size // 2:
current = self.head
for i in range(index):
current = current.next
else:
current = self.tail
for i in range(self.size - index - 1):
current = current.prev
return current.val
def addAtHead(self, val: int) -> None:
new_node = ListNode(val, None, self.head)
if self.head:
self.head.prev = new_node
else:
self.tail = new_node
self.head = new_node
self.size += 1
def addAtTail(self, val: int) -> None:
new_node = ListNode(val, self.tail, None)
if self.tail:
self.tail.next = new_node
else:
self.head = new_node
self.tail = new_node
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index < 0 or index > self.size:
return
if index == 0:
self.addAtHead(val)
elif index == self.size:
self.addAtTail(val)
else:
if index < self.size // 2:
current = self.head
for i in range(index - 1):
current = current.next
else:
current = self.tail
for i in range(self.size - index):
current = current.prev
new_node = ListNode(val, current, current.next)
current.next.prev = new_node
current.next = new_node
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
if index == 0:
self.head = self.head.next
if self.head:
self.head.prev = None
else:
self.tail = None
elif index == self.size - 1:
self.tail = self.tail.prev
if self.tail:
self.tail.next = None
else:
self.head = None
else:
if index < self.size // 2:
current = self.head
for i in range(index):
current = current.next
else:
current = self.tail
for i in range(self.size - index - 1):
current = current.prev
current.prev.next = current.next
current.next.prev = current.prev
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)
双链表实现,可能在空间复杂度稍高,在index、tail相关的处理稍快?
206.反转链表
代码随想录:206.反转链表
Leetcode:206.反转链表
直接做题
思路接近双指针,但更新逻辑没处理好。应该是先保存cur.next为temp,然后反转cur.next为prev,然后进行双指针的更新(cur = temp, pre = cur)。
看文章
双指针
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur = head
pre = None
while cur:
temp = cur.next # 保存一下cur的下一个节点,因为接下来要改变cur->next
cur.next = pre # 反转
#更新pre、cur指针
pre = cur
cur = temp
return pre
时间复杂度: O(n)
空间复杂度: O(1)
递归法
相当于把双指针中循环的部分转成递归。
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
return self.reverse(head, None)
def reverse(self, cur: ListNode, pre: ListNode) -> ListNode:
if cur == None:
return pre
temp = cur.next
cur.next = pre
return self.reverse(temp, cur)
时间复杂度: O(n),要递归处理链表的每个节点
空间复杂度: O(n),递归调用了 n 层栈空间
以往忽略的知识点小结
- 虚拟节点的使用
- class的写法
个人体会
完成时间:2h30min。
心得:看完觉得不难,但实际上链表仍然是薄弱点,需要加强。