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
相关推荐
何其有幸.18 分钟前
实验3-3 比较大小(PTA|C语言)
c语言·数据结构·算法
东阳马生架构1 小时前
Sentinel源码—8.限流算法和设计模式总结二
算法·设计模式·sentinel
老饼讲解-BP神经网络2 小时前
一篇入门之-评分卡变量分箱(卡方分箱、决策树分箱、KS分箱等)实操例子
算法·决策树·机器学习
何其有幸.2 小时前
实验6-3 使用函数求特殊a串数列和(PTA|C语言)
c语言·数据结构·算法
不会计算机的捞地2 小时前
【数据结构入门训练DAY-24】美国大选
数据结构·算法
明月看潮生3 小时前
青少年编程与数学 02-018 C++数据结构与算法 11课题、分治
c++·算法·青少年编程·编程与数学
Echo``3 小时前
2:QT联合HALCON编程—图像显示放大缩小
开发语言·c++·图像处理·qt·算法
.似水3 小时前
2025.4.22_C_可变参数列表
java·c语言·算法
Felven4 小时前
A. Ideal Generator
java·数据结构·算法
MoonBit月兔4 小时前
双周报Vol.70: 运算符重载语义变化、String API 改动、IDE Markdown 格式支持优化...多项更新升级!
ide·算法·哈希算法