Description

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.

解题:给一个链表,求倒数第n个结点的值。先贴一下自己的代码,思路比较简单,遍历两遍,第一遍算结点总数,推出所求结点是第几个,再遍历一次,得出答案。代码如下:

 /**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.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.
*/
public ListNode nthToLast(ListNode head, int n) {
// write your code here
int number = 0;
int count=0;
ListNode p = head;
while(p != null){
number++;
p = p.next;
}
p=head;
while(count != (number-n)){
count++;
p=p.next;
}
return p;
}
}

再贴一下另一中解法,稍微绕个弯子就可以了。定义两个游标,保证前一个与后一个差n,前一个到尾的时候,后一个就是所求值。代码如下:

  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;
}
}

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

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

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

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

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

  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. 166 链表倒数第n个结点

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

  7. Java Algorithm Problems

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

  8. 链表倒数第n个节点

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

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

随机推荐

  1. 课时48.表单标签-H5(了解)

    可以自动校验输入的内容是否符合邮箱的格式,我带大家随便看几个就行了,因为大部分浏览器对这几个特性有的支持,有的不支持,所以大家只要了解就好了   

  2. php开发微信图灵机器人

    本着开源为原则,为这个世界更美好作出一份共享,我就给大家做个指路人,如果实用,记得给提供开源的朋友一些鼓励. 简单介绍一下实现思路,使用swoole扩展接管php运行,由于swoole只能在类UNIX ...

  3. springBoot 官方整合的redis 使用教程:(StringRedisTemplate 方式存储 Object类型value)

    前言:最近新项目准备用 redis 简单的缓存 一些查询信息,以便第二次查询效率高一点. 项目框架:springBoot.java.maven  说明:edis存储的数据类型,key一般都是Strin ...

  4. 解决最新版 mac os sierra usb网卡不能使用的问题

    解决最新版 mac os sierra usb网卡不能使用的问题 解决最新版 mac os sierra usb网卡不能使用 无法使用未签名第三驱动的问题 我的情况是 mac os sierra 使用 ...

  5. 汇编中PSP是什么?为什么一般cs比ds大10h

    一般来说,PSP是256个字节,当程度生成了可执行文件以后,在执行的时候,先将程序调入内存, 这个时候DS中存入程序在内存中的段地址,紧接着是程序的一些说明,比如说程序占用多大空间等 等,这就是PSP ...

  6. C++练习 | 创建并正序输出不带头结点的链表

    #include <iostream> #include <cstdio> #include <stdlib.h> using namespace std; str ...

  7. 19-3-4 Python进制转换;bool str int三者之间的转换;字符串的索引,切片;for循环的使用

    进制转换: 二进制转十进制:  0010 1111 = 1*2**0+1*2**1+1*2**2+1*2**3+1*2**5 十进制转换二进制: 用十进制数除2逆序取余 --->101010 布 ...

  8. ATM购物作业

    一. 基本需求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录日常消费 ...

  9. Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore[转]

    [转载]http://www.cnblogs.com/dolphin0520/p/3920397.html 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDow ...

  10. SQL Server 2012 - 动态SQL查询

    动态SQL的两种执行方式:EXEC @sql 和 EXEC sys.sp_executesql @sql DECLARE @c_ids VARCHAR(200) SET @c_ids ='1,2' - ...