classListNode: def__init__(self, val=0, next=None): self.val = val self.next = next
2. 常见链表操作
2.1 遍历链表
遍历是最基本的链表操作,也是许多其他操作的基础。
1 2 3 4 5
deftraverse(head): current = head while current: print(current.val) current = current.next
2.2 插入节点
在链表中插入节点需要注意更新相关节点的指针。
1 2 3 4 5 6 7 8 9 10
definsert(head, val, position): dummy = ListNode(0) dummy.next = head current = dummy for _ inrange(position): current = current.next new_node = ListNode(val) new_node.next = current.next current.next = new_node return dummy.next
2.3 删除节点
删除节点同样需要注意更新相关节点的指针。
1 2 3 4 5 6 7 8
defdelete(head, position): dummy = ListNode(0) dummy.next = head current = dummy for _ inrange(position): current = current.next current.next = current.next.next return dummy.next
3. 常见链表问题解题技巧
3.1 双指针技巧
双指针是解决链表问题的常用技巧,特别是在处理环形链表、找中点等问题时非常有用。
快慢指针
用于检测环、找中点等。
1 2 3 4 5 6 7 8 9 10
defhasCycle(head): ifnot head ornot head.next: returnFalse slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: returnTrue returnFalse
间隔指针
用于找倒数第 K 个节点等。
1 2 3 4 5 6 7 8 9 10
deffindKthFromEnd(head, k): fast = slow = head for _ inrange(k): ifnot fast: returnNone fast = fast.next while fast: slow = slow.next fast = fast.next return slow
3.2 反转链表
反转链表是一个常见的操作,也是很多复杂链表问题的基础。
1 2 3 4 5 6 7 8 9
defreverseList(head): prev = None current = head while current: next_temp = current.next current.next = prev prev = current current = next_temp return prev
3.3 合并链表
合并两个有序链表是另一个常见的操作。
1 2 3 4 5 6 7 8 9 10 11 12 13
defmergeTwoLists(l1, l2): dummy = ListNode(0) current = dummy while l1 and l2: if l1.val < l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 if l1 else l2 return dummy.next
4. 高级技巧
4.1 递归
很多链表问题可以用递归来解决,常见的包括反转链表、合并链表等。
1 2 3 4 5 6 7
defreverseListRecursive(head): ifnot head ornot head.next: return head new_head = reverseListRecursive(head.next) head.next.next = head head.next = None return new_head
4.2 虚拟头节点
使用虚拟头节点可以简化很多操作,特别是在需要修改头节点的情况下。
1 2 3 4 5 6 7 8 9 10
defremoveElements(head, val): dummy = ListNode(0) dummy.next = head current = dummy while current.next: if current.next.val == val: current.next = current.next.next else: current = current.next return dummy.next