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 小时前
【AI编程】AI+高德MCP不到10分钟搞定上海三日游
人工智能·算法·程序员
mit6.8241 小时前
[Leetcode] 预处理 | 多叉树bfs | 格雷编码 | static_cast | 矩阵对角线
算法
皮卡蛋炒饭.2 小时前
数据结构—排序
数据结构·算法·排序算法
??tobenewyorker3 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode
贝塔西塔3 小时前
一文读懂动态规划:多种经典问题和思路
算法·leetcode·动态规划
众链网络3 小时前
AI进化论08:机器学习的崛起——数据和算法的“二人转”,AI“闷声发大财”
人工智能·算法·机器学习
3 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
飒飒真编程5 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法
GeminiGlory5 小时前
算法练习6-大数乘法(高精度乘法)
算法
熬了夜的程序员5 小时前
【华为机试】HJ61 放苹果
算法·华为·面试·golang