LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List
Total Accepted: 46720 Total Submissions: 168596My Submissions
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.
SOLUTION 1:
1.使用快慢指针,快指针先行移动N步。用慢指针指向要移除的Node的前一个Node.
2. 使用dummy node作为head的前缀节点,这样就算是删除head也能轻松handle啦!
主页君是不是很聪明呀? :)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//
ListNode dummy = new ListNode();
dummy.next = head; ListNode slow = dummy;
ListNode fast = dummy; // move fast N more than slow.
while (n > ) {
fast = fast.next;
// Bug 1: FORGET THE N--;
n--;
} while (fast.next != null) {
fast = fast.next;
slow = slow.next;
} // Slow is the pre node of the node which we want to delete.
slow.next = slow.next.next; return dummy.next;
}
}
GITHUB (国内用户可能无法连接):
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 @ 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] 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 = ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- LeetCode 237 Delete Node in a Linked List 解题报告
题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...
随机推荐
- linux系统基础网络配置
1.修改主机名 临时方法: 退出当前shell重新登录即可生效.此法只能临时修改生效.重启系统后失效. 提示:很多人使用hostname主机名来修改,其实这个只是作为暂时的.重启后将恢复到配置前的主机 ...
- 让asp.net和php同时在Linux上跑起来
实现这个想法有很多种方法,我就我最近算是比较熟悉的方式来说给大家吧. 因为有了mono,所以.net才能跨平台,.net都跨平台了,.net平台的童鞋们是不是应该提起精神好好了解下mono呢? 当然了 ...
- 把HDFS里的json数据转换成csv格式
1. 全景图 2. 用ListHDFS获取所有文件名 如果想重新再取一次,右健view state: 点击 clear state, 再运行,即可再次采集数据了. 3. 用FetchH ...
- 教务管理系统数据库E/R图
- MySQL5.6 报错1067
http://www.cnblogs.com/lixiaolun/p/5303687.html
- dubbo配置文件报错解决思路
导入dubbo项目到Eclipse,配置文件报了如下异常: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The ...
- httpwebrequest 请求压缩,接受压缩的字符流请求
请看图,客户端和服务端都使用gzip压缩. 客户端参数以json字符串形式gzip压缩,以二进制流发送到服务端:服务端接收到请求,解压缩gzip后,进行业务逻辑处理:处理完后在将返回值以json形式, ...
- 在Mac OS上安装Vagrant和Docker的教程
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/128.html?1455808640 当听到很多人在说Docker是多么多 ...
- linux下安装tomcat,部署项目
一.先检查是否安装jdk java -version 查看版本信息,如果显示 那基本就是安装了. root@iZ2ze2hksqphzp0eh7dskoZ:/opt# java -version -b ...
- Mybatis多参传递的四种解决方案
Mybatis多参传递的四种解决方案 代码异常:org.apache.ibatis.binding.BindingException: Parameter 'param' not found. 长时间 ...