[leetcode]_Remove Nth Node From End of List
题目:移除linked-list从尾到头的第N个元素
自我思路:因为题目给出的N是从链表尾开始计算的,单链表不存在从子指向父亲的反向指针,因此我先计算链表的整个长度len,然后用len - N来表示正向被删除元素所在的位置。
代码:
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null;
ListNode help = head;
int length = 1;
while(help.next != null){
length++;
help = help.next;
}
if(n == length){
head = head.next;
}else{
ListNode temp = head;
for(int i = 1 ; i < length - n ; i++) temp = temp.next;
if(n == 1) temp.next = null;
else temp.next = temp.next.next;
}
return head;
}
这样的算法有个很低效的地方,如果我要删除一个very very 长的链表的倒数第二个元素,我得先遍历一遍,这一步可能半小时过去了。然后我又得查找一遍,才能删除。
网络上的思路非常精辟,双指针(快指针 & 慢指针),虽然原理还没有很理解,但是举例这样的确能得到正确答案。快指针比慢指针先走N步,N步以后快慢指针同时继续走,当快指针走到链表尾部时,慢指针指向的就是被删除元素的前一个元素。精妙的思路。
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null) return null;
ListNode fast = head;
ListNode slow = head;
int fastGo = n;
while(n > 0 && fast.next != null){
fast = fast.next;
n--;
}
if( n == 0 ){
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
if(slow.next.next != null) slow.next = slow.next.next;
else slow.next = null;
}else{
head = head.next;
}
return head;
}
[leetcode]_Remove Nth Node From End of List的更多相关文章
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- [leetcode]Remove Nth Node From End of List @ Python
原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...
- LeetCode——Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- Leetcode Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [LeetCode] Remove Nth Node From End of List 快慢指针
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- LeetCode OJ-- Remove Nth Node From End of List
https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
随机推荐
- 华硕X84L无线驱动查找
打开官网:http://www.asus.com.cn/ 点击导航栏的服务与支持 产品型号识别http://www.asus.com.cn/support/Article/565/ 我的是:X84L ...
- 前后台彻底分离的核心文件bridge.js.
具体代码可以在我的git上下载:https://github.com/guoyansi/bridge 这里的后台使用java写的,如果不了解java的童鞋可以忽略下面这样图片. bridge.js / ...
- 最最基层的ajax交互代码jquery+java之间的json跨域传递以及java的json代码返回
首先导入jar包 上面的jar包主要是用来将map或list数据转换成json字符串,传递到前台使用. 静态页面的代码:2.html <!DOCTYPE html> <html> ...
- Centos安装(更新)git(亲测有效)
Centos 6.5默认安装的是git 1.7.X 版本,使用过程中会有一些奇怪的问题,对于用户名.密码支持不是很友好.将Centos6.5上的git更新到2.0.5,方法如下: 1.安装编译git时 ...
- Sqlserver中存储过程,触发器,自定义函数(一)
Sqlserver中存储过程,触发器,自定义函数 1.存储过程有关内容存储过程的定义:存储过程的分类:存储过程的创建,修改,执行:存储过程中参数的传递,返回与接收:存储过程的返回值:存储过程使用游标. ...
- (转)C# 解析 json
原文: http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html JSON(全称为JavaScript Object N ...
- 编码中常用的SQL语法
蓝色标注的都是比较常见的SQL ====================== 开发中常见的SQL: left join , right join 防止丢弃数据 inner join CASE WHNE ...
- 转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事
原文地址:http://msopentech.com/zh-hans/blog/2014/05/09/cocos2d-x-%E6%B8%B8%E6%88%8F%E6%8E%A5%E5%85%A5-wi ...
- 剑指Offer:面试题12——打印1到最大的n位数(java实现)
问题描述: 输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1,2,3一直到最大的3位数即999. 思路1:最简单的想法就是先找出最大的n位数,然后循环打印即可. public ...
- Google Protocol Buffer 的编码方式
Google Protocol Buffer 使用到了两种编码方式:Varints 和 zigzag. 一 Varints 编码 每个 byte 只用 7bit 表示数字,最高位 bit作为标志位,如 ...