Lintcode: Nth to Last Node in List
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example
Given a List ->->->->null and n = , return node whose value is .
Runner Technique: 两个指针都从Dummy node出发,结束条件是runner.next!=null
public class Solution {
/**
* @param head: The first node of linked list.
* @param n: An integer.
* @return: Nth to last node of a singly linked list.
*/
ListNode nthToLast(ListNode head, int n) {
// write your code here
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode walker = dummy;
ListNode runner = dummy;
while (runner.next != null && n>0) {
runner = runner.next;
n--;
}
while (runner.next != null) {
runner = runner.next;
walker = walker.next;
}
return walker.next;
}
}
Lintcode: Nth to Last Node in List的更多相关文章
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- Nth to Last Node in List
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Exam ...
- 166. Nth to Last Node in List
Description Find the nth to last element of a singly linked list. The minimum number of nodes in lis ...
- 【Lintcode】087.Remove Node in Binary Search Tree
题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- LintCode之链表倒数第n个节点
题目描述: 我的代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * L ...
- LintCode 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...
- 166 链表倒数第n个结点
原题网址:https://www.lintcode.com/problem/nth-to-last-node-in-list/description 描述 找到单链表倒数第n个节点,保证链表中节点的最 ...
随机推荐
- 7.PHP内核探索:Apache模块介绍
Apache概述 Apache是目前世界上使用最为广泛的一种Web Server,它以跨平台.高效和稳定而闻名.按照去年官方统计的数据,Apache服务器的装机量占该市场60%以上的份额.尤其是在 X ...
- PHP关闭提示、打印配置
打印配置 PHP.exe -i > Info.txt 关闭 PHP 提示的方法 搜索php.ini: error_reporting = E_ALL 改为: error_reporting = ...
- css中IE6fix问题
.g-popup-mask, .g-popup-box { position:fixed; top:0; left:0; z-index:10000; _position:absolute; _top ...
- ubuntu 制作deb 包
ubuntu下打包制作deb安装包 http://www.th7.cn/system/lin/201406/61012.shtml 2014-06-22 20:16:45CSDN-yangbing ...
- transform: translateY(-50%) 实现元素垂直居中效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NSNotificationCenter带参
(1)post Notification类 [[NSNotificationCenter defaultCenter] postNotificationName:CRMPerformanceNewCe ...
- oracle语句随笔
oracle语句随笔 dmp数据的导入. ; --创建用户 GRANT CONNECT,RESOURCE,DBA TO memsspc; --赋值权限 --cmd 中导入命令 IMP memsspc@ ...
- sublime3使用
http://dengo.org/archives/923 这篇博客的写的很好!
- iOS:BitCode的介绍
一.什么是BitCode?作用是什么? Bitcode is an intermediate representation of a compiled program. Apps you upload ...
- 微信公众平台开发(68)苹果IMEI查询
微信公众平台开发 苹果IMEI查询 苹果序列号查询 iPhone/iPad/iPod/Mac 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/weixin69 ...