删除单链表倒数第n个节点
基本问题
如何删除单链表中的倒数第n个节点?
常规解法
先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点。
代码实现
/**** Description: 删除单链表倒数第n个节点,常规解法.** @param head* @param n* @return ListNode*/public static ListNode removeNthFromEnd(ListNode head, int n) {if (head == null)return null;//get length of listListNode p = head;int len = 0;while (p != null) {len++;p = p.next;}//if remove first nodeint fromStart = len - n + 1;if (fromStart == 1)return head.next;//remove non-first nodep = head;int i = 0;while (p != null) {i++;if (i == fromStart - 1) {p.next = p.next.next;}p = p.next;}return head;}
一次遍历法
使用快慢指针。快指针比慢指针提前n个单元。当快指针到达单链表尾部时,慢指针指向待删除节点的前节点。
代码实现
/**** Description: 删除单链表倒数第n个节点,快慢指针法.** @param head* @param n* @return ListNode*/public static ListNode removeNthFromEnd(ListNode head, int n) {if (head == null)return null;ListNode fast = head;ListNode slow = head;for (int i = 0; i < n; i++) {fast = fast.next;}//if remove the first nodeif (fast == null) {head = head.next;return head;}while (fast.next != null) {fast = fast.next;slow = slow.next;}slow.next = slow.next.next;return head;}
删除单链表倒数第n个节点的更多相关文章
- 【链表问题】打卡2:删除单链表的第 K个节点
前言 以专题的形式更新刷题贴,欢迎跟我一起学习刷题.每道题会提供简单的解答. 题目描述 在单链表中删除倒数第 K 个节点 要求 如果链表的长度为 N, 时间复杂度达到 O(N), 额外空间复杂度达到 ...
- leetcode 去除单链表倒数第k个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 单链表倒数第K个节点的查找和显示
1.使用一个固定长度队列装链表段,当遍历到链表根时,返回队列头元素. class Node{ int value; Node next; public Node(int value){ this.va ...
- 19. Remove Nth Node From End of List【Medium】【删除单链表倒数第n个结点】
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
- LintCode 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
随机推荐
- mac安装 配置 ant
转自:http://blog.sina.com.cn/s/blog_877e9c3c0101qs87.html 1.下载ant 官网下载 http://ant.apache.org/bindownlo ...
- UNITY3D在IOS开发下的反射机制限制
IOS上的反射是部分支持,支持使用反射读取源代码,但不支持使用反射动态生成可执行代码,下面是限制反射的命名空间:ProfilerReflection.EmitReflection.Emit.Save ...
- Python 中的进程、线程、协程、同步、异步、回调
进程和线程究竟是什么东西?传统网络服务模型是如何工作的?协程和线程的关系和区别有哪些?IO过程在什么时间发生? 一.上下文切换技术 简述 在进一步之前,让我们先回顾一下各种上下文切换技术. 不过首先说 ...
- 配置svn
Linux搭建SVN Server 其中包括添加防火墙规则
- [SQL Server]Index/deadlock
http://www.cnblogs.com/tintown/archive/2005/04/24/144272.html http://coolshell.cn/ http://blogs.msdn ...
- osgEarth基础入门
osgEarth基础入门 2015年3月21日 16:19 osgEarth是基于三维引擎osg开发的三维数字地球引擎库,在osg基础上实现了瓦片调度插件,可选的四叉树调度插件,更多的地理数据加载插件 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- .NET面试必备(整理)
1.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问.public : 公共成员,完全公开,没有 ...
- ORA-12170:TNS:连接超时
本文转自 http://www.cnblogs.com/kerrycode/archive/2012/12/14/2818421.html 1:首先检查网络是否能ping通 2:检查TNS配置(TNS ...
- tar exclue文件夹
tar zcvf logs.tar.gz logs --exclude=logs/log1