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的更多相关文章

  1. lintcode :nth to Last Node In List 链表倒数第n个节点

    题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...

  2. 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 ...

  3. 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 ...

  4. 【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 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  7. LintCode之链表倒数第n个节点

    题目描述: 我的代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * L ...

  8. LintCode 链表倒数第n个节点

    找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...

  9. 166 链表倒数第n个结点

    原题网址:https://www.lintcode.com/problem/nth-to-last-node-in-list/description 描述 找到单链表倒数第n个节点,保证链表中节点的最 ...

随机推荐

  1. 我的第一个chrome扩展(0)——目标

    当前有两个方向: 一.实现一个自动解码的地址栏监视器 扩展程序在后台不断监视地址栏输入,地址栏输入并回车后检查输入,若输入符合解码条件则调用网站信息进行解码,并将结果输出到地址栏,否则不改变: 初始阶 ...

  2. sqlserver 获取当前操作的数据库名称

    Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = ...

  3. Ubuntu 设置Vim tab为四个空格

    使用root权限打开 /etc/vim/vimrc 添加下列配置 set tabstop= set softtabstop= set shiftwidth= set noexpandtab set n ...

  4. style="display"之后不能获取offsetHeight或clientWidth这类测量的值

    如果在html元素中设置了style="display:none;height:90px;"的属性后,是无法获得offsetLeft ,offsetWidth,offsetTop, ...

  5. [LeetCode]题解(python):041-First Missing Positive

    题目来源 https://leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the ...

  6. JMeter学习-016-思路篇之-山重水复柳暗花明

    首先,此文非技术类博文,为思路类的博文,敬请参阅,欢迎共同探讨! 今天在编写 JMeter 接口监控脚本时,遇到了一个问题,在解决问题的时候,思路出现了偏差,导致了自己在解决问题时,绕了弯,浪费了些时 ...

  7. Fiddler-008-简单模拟性能测试

    通过 Fiddler 可以简单的模拟性能测试的并发测试,此方法非常的简单,直接讲述如何使用,敬请参阅! 首先我们要获取需要并发的 HTTP请求,此操作非常简单,则在此不再赘述.获取到响应的 HTTP请 ...

  8. CSS布局一

    CSS布局一 实例一(居中) div#container{ width:960px; height:650px; margin:0 auto; border:1px solid #ccc; /*就是说 ...

  9. 配置SQL Server Session方法

    以下过程是在Win 2003 SP2 + IIS 6.0, ASP.NET 2.0, SQL Server 2005下进行的. 1. 安装Session数据库到Framework目录 C:\WINDO ...

  10. 关于使用"/"来 dispatcherServlet 的url-pattern带来的问题

    之前一直使用*.do来做的,但是绝的*.do很丑,于是就改用"/"来配置: <servlet> <servlet-name>dispatcherServle ...