[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 = ...
随机推荐
- 服务器压力测试 ab
1.ab命令原理 Apache自带的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力(测试其它服务器时需单独下载ab压力测试工具 ...
- 栅格系统不是要包裹在container里面吗
栅格系统不是要包裹在container里面吗 为什么直接设置col-sm 在form表单中 不是应该这样么 <form> <div class="container&quo ...
- 如何判断VS.NET设计时?
方法一: if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") //...Then ...
- Python第九章模块和包
1.import Python文件的时候文件名不能跟Python中自带的关键字重复,否则无法使用关键字的方法. 2.Reload(),重载例子 import sysreload(sys)sys.set ...
- BC水题--The Factor(质因分解)
网址:http://acm.hdu.edu.cn/showproblem.php?pid=5428 roblem Description There is a sequence of n positi ...
- python 实例属性之单,双下划线
具体区别看下面例子 class A: def __init__(self,name='Andy'): self._name = name class B: def __init__(self,name ...
- android 提高进程优先级 拍照永不崩溃(闪退)
首先科普一下Android系统进程的优先级: 当系统的内存不足时, android系统将根据进程优先级选择杀死一些不太重要的进程. 进程优先级从高到低分别为: 1. 前台进程. 以下的进程为前台进程: ...
- CSS3里的常用选择器总结
选择器 属性选择器: img[src="images/2.jpg"] 开头匹配: a[href ^="page/"] ...
- JS获取当前文件所在的文件夹全路径
var js = document.scripts; js = js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf ...
- 洛谷P1631 序列合并
P1631 序列合并 236通过 657提交 题目提供者xmyzwls 标签堆 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 为什么不行? 题目描述 有两个长度都是N的序列A和B,在A和B中 ...