利用双指针一次遍历实现”找到“并”删除“单链表倒数第K个节点(力扣题目为例)

Problem: 19. 删除链表的倒数第 N 个结点

文章目录

题目描述


思路

1.欲找到倒数第k个节点,即是找到正数的第n-k+1、其中n为单链表中节点的个数 个节点。

2.为实现只遍历一次单链表,我们先可以使一个指针p1指向链表头部再让其先走k步,此时再让一个指针p2指向单链表的头部接着使其同p1一起往后走,当p1指向单链表的尾部空指针时(即p1 = null)时停止,此时p2指向的即为正数n-k+1 个节点也即使倒数第k个节点;

3.但是在单链表的删除 中我们需要找到待删除节点的前驱节点 我们在第二步中只是实现了找到倒数第k个节点 离删除它还差一步,那我们就先找出倒数第k+1个节点再删除倒数第k个节点

复杂度

时间复杂度:

O ( n ) O(n) O(n);

空间复杂度:

O ( 1 ) O(1) O(1)

Code

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        // virtual head node
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode p = dummy;
        // find the (n + 1) th node from the end
        // then we can remove the n-th node from the end
        ListNode x = findFromEnd(dummy, n + 1);
        // remove the n-th node from the end
        x.next = x.next.next;
        return dummy.next;
    }
    
    // return the k-th node from the end of the linked list
    ListNode findFromEnd(ListNode head, int k) {
        ListNode p1 = head;
        // p1 moves k steps firstly
        for (int i = 0; i < k; ++i) {
            p1 = p1.next;
        }
        ListNode p2 = head;
        // p1 and p2 move n - k steps together
        while (p1 != null) {
            p2 = p2.next;
            p1 = p1.next;
        }
        // p2 is now pointing to the (n - k + 1) -th node,which is the k-th node from the end
        return p2;
    }
}
相关推荐
洛水水20 小时前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI20 小时前
聚类算法详解
算法·数据挖掘·聚类
刀法如飞21 小时前
Go 字符串查找的 20 种实现方式,用不同思路解决问题
算法·面试·程序员
Dlrb12111 天前
C语言-指针数组与数组指针
c语言·数据结构·算法·指针·数组指针·指针数组·二级指针
WL_Aurora1 天前
Python 算法基础篇之集合
python·算法
平行侠1 天前
A15 工业路由器IP前缀高速检索与内存压缩系统
网络·tcp/ip·算法
阿旭超级学得完1 天前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表
li星野1 天前
位运算 & 数学 & 高频进阶九题通关(Python + C++)
c++·python·学习·算法
jerryinwuhan1 天前
hello算法,简单讲(1)
算法·排序算法
y = xⁿ1 天前
20天速通LeetCodeday15:BFS广度优先搜索
算法·宽度优先