LeetCode OJ 292.Nim Gam19. 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,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
最近特别喜欢解决链表方面的问题,感觉指针指来指去还是挺有意思的,而且在解决指针问题时有好多技巧来降低时间复杂度和空间复杂度。
上面这个题目就是一个比较典型的用双指针来解决问题的例子。按照正常的想法:一次遍历怎么可能做到定位这个指针呢?小白的想法是先计算指针的长度length吧,然后从前往后遍历(length-n)个节点,则下一个节点就是我们要删除的节点。这样最起码要遍历两遍。
如果我们有两个指针,一个快指针和一个慢指针,快指针比慢指针快(n-1)步,那么如果快指针.next==null时,慢指针正好指向那个我们要删除的指针。这样一次遍历就能完成这个问题。怎么样?这个方法是不是很巧妙呢?代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(n <= 0 || head==null) return head;
ListNode fast = head; //快指针
ListNode slow = head; //慢指针
ListNode pre = null; while(fast.next != null){
if(n <= 1){ //快指针比慢指针快n-1步
pre = slow;
slow = slow.next;
}
fast = fast.next;
n--;
}
if(slow == head) head = head.next;//如果删除的是头指针
else pre.next = slow.next; //删除的不是头指针
return head;
}
}
LeetCode OJ 292.Nim Gam19. Remove Nth Node From End of List的更多相关文章
- 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)
Given a linked list, remove the n-th node from the end of list and return its head. Example: ...
- 【Leetcode】【Easy】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 292.Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- LeetCode OJ 292.Nim Gam148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种 ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
随机推荐
- [bash] 查找替换文件
写这个脚本也加深了对 bash 数组的理解. #!/bin/bash #2015-11-23 echo -e "说明:\n将文件放在/app/tmp_class目录下,保证该目录下没有其他文 ...
- Java内部类的使用小结
转载:http://android.blog.51cto.com/268543/384844/ Java内部类的使用小结 内部类是指在一个外部类的内部再定义一个类.类名不需要和文件夹相同. *内部类可 ...
- 【转】母函数(Generating function)详解 — TankyWoo(红色字体为批注)
母函数(Generating function)详解 - Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...
- A. Grasshopper And the String(CF ROUND 378 DIV2)
A. Grasshopper And the String time limit per test 1 second memory limit per test 256 megabytes input ...
- 解决 GoogleApi 无法访问的问题
因为 google 被天朝屏蔽,所以很多运用了 fonts.googleapis 的网站都打开很慢,会直到加载 fonts.googleapis 超时才能打开网页. 在本地开发时,可以引用国内的CDN ...
- Ajax的基本请求/响应模型
一.Ajax工作核心 Ajax的核心是JavaScript对象XMLHttpRequest(简称XHR).它是一种支持异步请求的技术.可以通过使用XHR对象向服务器提出请求并处理响应,而不阻塞用户. ...
- iosTableView 局部全部刷新以及删除编辑操作
局部刷新方法 添加数据 NSArray *indexPaths = @[ [NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath index ...
- ValidationMessageFor验证
一,在modle下的自己新建的PowerViewModel.cs下编辑错误显示 public class PowerViewModel { [Required( ErrorMess ...
- GOPS 2016全球运维大会 • 北京站概况
GOPS 2016全球运维大会上海站已圆满落幕,错过上海站的朋友或许会感到一些遗憾,但是不用担心,在12月16日,GOPS 2016全球运维大会 • 北京站将隆重召开,错过上海在的朋友可以赶上北京站哦 ...
- 苹果4S
港版.4S.白.非翻新机.16G.联通3G移动2G电信2G 1000 美版.4S.白.翻新.16G.联通3G移动2G电信3G 980