Leetcode-100 链表常见操作

链表常见操作总结

1. 链表的定义

单链表节点定义

python 复制代码
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

双向链表节点定义

python 复制代码
class DoublyListNode:
    def __init__(self, val=0, prev=None, next=None):
        self.val = val
        self.prev = prev
        self.next = next

2. 链表基本操作

1 插入节点

头插法
python 复制代码
def insert_at_head(head, val):
    new_node = ListNode(val)
    new_node.next = head
    return new_node  # 返回新的头节点
尾插法
python 复制代码
def insert_at_tail(head, val):
    new_node = ListNode(val)
    if not head:
        return new_node
    cur = head
    while cur.next:
        cur = cur.next
    cur.next = new_node
    return head

2 删除节点

删除指定值的节点
python 复制代码
def delete_node(head, val):
    dummy = ListNode(0)
    dummy.next = head
    prev, cur = dummy, head
    while cur:
        if cur.val == val:
            prev.next = cur.next
            break
        prev, cur = cur, cur.next
    return dummy.next

3 反转链表

反转整个链表

python 复制代码
def reverse_list(head):
    prev, cur = None, head
    while cur:
        next_node = cur.next
        cur.next = prev
        prev = cur
        cur = next_node
    return prev  # 新的头节点

反转长度为k的链表(k个一组反转链表)

python 复制代码
def reverseKGroup(head, k):
    # 先检查是否有至少 k 个节点
    temp, count = head, 0
    while temp and count < k:
        temp = temp.next
        count += 1

    # 如果长度不足 k,则直接返回原链表头部
    if count < k:
        return head

    # 反转 k 个节点
    prev, curr = None, head
    for _ in range(k):
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node

    # 递归处理后续的部分,并连接
    head.next = reverseKGroup(curr, k)
    return prev  # 返回翻转后的新头节点

4 快慢指针

查找中间节点

python 复制代码
def find_middle(head):
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
    return slow

检测链表是否有环

python 复制代码
def has_cycle(head):
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

找到环的起点

python 复制代码
def detect_cycle(head):
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            ptr = head
            while ptr != slow:
                ptr = ptr.next
                slow = slow.next
            return ptr  # 环的起点
    return None

5 其他

合并两个有序链表

python 复制代码
def merge_two_lists(l1, l2):
    dummy = ListNode(0)
    cur = dummy
    while l1 and l2:
        if l1.val < l2.val:
            cur.next, l1 = l1, l1.next
        else:
            cur.next, l2 = l2, l2.next
        cur = cur.next
    cur.next = l1 if l1 else l2
    return dummy.next

分割链表

python 复制代码
def splitList(head, size):
    # 先找到 next_head 的前一个节点
    cur = head
    for _ in range(size - 1):
        if cur is None:
            break
        cur = cur.next

    # 如果链表长度 <= size
    if cur is None or cur.next is None:
        return None  # 不做任何操作,返回空节点

    next_head = cur.next
    cur.next = None  # 断开 next_head 的前一个节点和 next_head 的连接
    return next_head

判断两个链表是否相交

python 复制代码
def get_intersection_node(headA, headB):
    if not headA or not headB:
        return None
    a, b = headA, headB
    while a != b:
        a = a.next if a else headB
        b = b.next if b else headA
    return a
相关推荐
聚客AI12 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v15 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工17 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农18 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了19 小时前
AcWing学习——双指针算法
c++·算法
moonlifesudo19 小时前
322:零钱兑换(三种方法)
算法
NAGNIP2 天前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队2 天前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法