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
相关推荐
熬了夜的程序员1 小时前
【LeetCode】114. 二叉树展开为链表
leetcode·链表·深度优先
大胆飞猪5 小时前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
Kisorge7 小时前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
铭哥的编程日记8 小时前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区8 小时前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
cici158748 小时前
基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现
算法·matlab·最小二乘法
StarPrayers.9 小时前
自蒸馏学习方法
人工智能·算法·学习方法
大锦终9 小时前
【动规】背包问题
c++·算法·动态规划
智者知已应修善业10 小时前
【c语言蓝桥杯计算卡片题】2023-2-12
c语言·c++·经验分享·笔记·算法·蓝桥杯
hansang_IR10 小时前
【题解】洛谷 P2330 [SCOI2005] 繁忙的都市 [生成树]
c++·算法·最小生成树