Find the nth to last element of a singly linked list.

The minimum number of nodes in list is n.

Example

Given a List  3->2->1->5->null and n = 2, return node  whose value is 1.

分析:

要找到nth to last element,我们需要两个指针,第一个指针先走n步,然后两个指针同时走,知道第一个指针为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) {
if (head == null || n <= ) return null; ListNode first = head;
ListNode last = head; for (int count = ; count <= n; count++;) {
first = first.next;
} while(first != null) {
first = first.next;
last = last.next;
}
return last;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Nth to Last Node in List的更多相关文章

  1. 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. Exam ...

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

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

  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]——目录

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

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

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

  6. 链表倒数第n个节点

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

  7. Solutions and Summay for Linked List Naive and Easy Questions

    1.Remove Linked List Elements package linkedlist; /* * Question: Remove all elements from a linked l ...

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

    链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...

  9. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

随机推荐

  1. ansible 小试身手

    我们安装好了ansible之后 配置了免密码登陆 现在我们可以检查一下管理主机和被管理主机的连通性 ansible   all   -m   ping 在我们的实际生产中我们倾向于使用普通用户用sud ...

  2. 通过smtp协议简单实现邮件发送

    使用到的类: ①SmtpClient--发送邮件的类(using System.Net.Mail;) ②MailMessage--初始化邮件的类 ③ NetworkCredential--身份验证的类 ...

  3. 【转】Eclipse下导入外部jar包的3种方式

    我们在用Eclipse开发程序的时候,经常要用到第三方jar包.引入jar包不是一个小问题,由于jar包位置不清楚,而浪费时间.下面配图说明3种Eclipse引入jar包的方式.   1.最常用的普通 ...

  4. NOI题库--图论 宗教信仰

    1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...

  5. asp.net 回发或回调参数无效的各种情况分析及解决办法

    昨天,在实现级联菜单的时候,突然出现一下错误: 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或在页面中 ...

  6. CVE-2014-0160 Heartbleed Vul Analysis && OpenSSL Cryptographic Software Library Bug

    目录 . Heartbleed漏洞简介 . 漏洞造成的风险和影响 . 漏洞的测试.POC . OpenSSL漏洞源代码分析 . 防御.修复方案 . 从漏洞中得到的攻防思考 1. Heartbleed漏 ...

  7. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  8. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

  9. android.net.wifi的简单使用方法

    获取Wifi的控制类WifiManager.  WifiManager  wm=(WifiManager)getSystemService(Context.WIFI_SERVICE); 接下来可以对w ...

  10. mvc中EditorFor TextBoxFor什么区别

    EditorFor 是映射到Model 属性上面,忽略用户自定义属性和样式 Model 可以为nullTextBoxFor是映射到Model 属性上面,可以用户自定义属性和样式 Model 不可以为n ...