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 ...
随机推荐
- Win7 远程 Ubuntu 桌面 mate desktop,并实现中文输入法
一. 安装mate desktop: $sudo apt-add-repository ppa:ubuntu-mate-dev/ppa $sudo apt-add-repository ppa:ubu ...
- mono for android代码记录1 WebClient Post数据到Nopcommerce(post数据到MVC5)
WebClient Post数据到Nopcommerce(post数据到MVC5) 注意事项 1.把[ValidateAntiForgeryToken]注释掉,以后再处理CSRF攻击.现在学习先不理 ...
- SQL Develop SSH远程连接
目前碰到了本机无法直接连接数据库服务器,然后mac上可以选择的oracle客户端又很少,直到知道了可以ssh远程到跳板机上,然后进行连接. 在跳板机上输入命令如下: ssh -L 0.0.0.0:65 ...
- 重新理解JS的6种继承方式
写在前面 一直不喜欢JS的OOP,在学习阶段好像也用不到,总觉得JS的OOP不伦不类的,可能是因为先接触了Java,所以对JS的OO部分有些抵触. 偏见归偏见,既然面试官问到了JS的OOP,那么说明这 ...
- 设计模式之美:Structural Patterns(结构型模式)
结构型模式涉及到如何组合类和对象以获得更大的结构. 结构型类模式采用继承机制来组合接口实现. 结构型对象模式不是对接口和实现进行组合,而是描述了如何对一些对象进行组合,从而实现新功能的一些方法. 因为 ...
- Server Develop (九) Simple Web Server
Simple Web Server web服务器hello world!-----简单的socket通信实现. HTTP HTTP是Web浏览器与Web服务器之间通信的标准协议,HTTP指明了客户端如 ...
- [ucgui] 对话框8——Framewin小工具
>_<" 这里主要是窗口小工具Framewin的用法介绍,包括创建背景窗口及其消息回调函数,创建Frame窗口并设置其回调函数,这里一些其他的功能被我注释掉了,有向状态栏添加最大 ...
- AngularJS 源码分析3
本文接着上一篇讲 上一篇地址 回顾 上次说到了rootScope里的$watch方法中的解析监控表达式,即而引出了对parse的分析,今天我们接着这里继续挖代码. $watch续 先上一块$watch ...
- .net中对象序列化技术浅谈
.net中对象序列化技术浅谈 2009-03-11 阅读2756评论2 序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储 ...
- 从题目中学习java语法
一.输入输出 1.输入圆的半径,计算并输出圆的周长和面积: import java.util.Scanner; public class zuoye01_circle { public static ...