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 ...
随机推荐
- 6.4 APK包限制
Google 2015年 9月28日公告:为了Android开发商可以制作出更加复杂的app和游戏,Google Play游戏包体(APK)大小由原来的50MB提高到100MB.但是针对Android ...
- Xshell_Using X11 forwarding
FROM:http://www.netsarang.com/tutorial/xshell/1018/Using_X11_forwarding The X11 forwarding feature i ...
- Rsync 3.1.0 发布,文件同步工具
文件同步工具Rsync 3.1.0发布.2013-09-29 上一个版本还是2011-09-23的3.0.9 过了2年多.Rsync基本是Linux上文件同步的标准了,也可以和inotify配合做实时 ...
- 解决linux下oracle-11g打不开dbca问题
linux下oracle安装完毕后,出现建立数据库命令dbca无法使用问题,如图: 解决办法: 在32位的linux环境下,安装32位oracle11g会有这个bug,主要装个补丁(patch nam ...
- Mac OS X 安装ruby环境
1.查看版本 $ ruby -v ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14] 2.查看源 $ gem ...
- [51单片机] EEPROM 24c02 + 数码管 + 中断 [统计开机次数]
>_<:24c02的SCL连P2.0;SDA连P2.1;WP接GND;P0接8位数码管的8针;P2.2连段码;P2.3连位码; >_<:delay.c #include &qu ...
- 虚拟机VirtualBox中centos6.5网络设置
一.虚拟机网络配置 默认只是设置了网卡1:方式NAT(对应ifcfg-eth0) 我们还可以设置网卡2,网卡3.可以在系统安装完成后设置. 网卡2设置回环网卡,实现虚拟机与宿主机组成局域网(对应ifc ...
- [数据库操作]Java中的JDBC的使用方法.
前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...
- [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler
[原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...
- js 生成笛卡尔积
其实生成 笛卡尔积的方法原本很简单,for循环就可以了, function discarts() { //笛卡尔积 var twodDscartes = function (a, b) { var r ...