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
相关推荐
拓端研究室2 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
随缘而动,随遇而安4 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
IT古董4 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
Alfred king7 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
水木兰亭7 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
Jess078 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁8 小时前
选择排序算法详解
数据结构·算法·排序算法
xindafu8 小时前
代码随想录算法训练营第四十二天|动态规划part9
算法·动态规划
xindafu8 小时前
代码随想录算法训练营第四十五天|动态规划part12
算法·动态规划
ysa0510309 小时前
Dijkstra 算法#图论
数据结构·算法·图论