-----------------------------------

最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了。

AC代码:

/**
* 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.
*/
ListNode nthToLast(ListNode head, int n) {
int length=0;
ListNode node=head;
while(node!=null){
length++;
node=node.next;
}
for(int i=length-n;i>0;i--){
head=head.next;
}
return head;
}
}

然后神奇的快慢指针出场打脸了...

两个指针间距n(慢指针+n=快指针),当快指针到达尾部的时候慢指针所处的位置就是我们需要的啦。

AC代码:

/**
* 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.
*/
ListNode nthToLast(ListNode head, int n) {
ListNode fast, slow;
fast=slow=head; while(n-->0) fast=fast.next; while(fast!=null){
fast=fast.next;
slow=slow.next;
} return slow;
}
}

题目来源: http://www.lintcode.com/zh-cn/problem/nth-to-last-node-in-list/

Lintcode 166. 链表倒数第n个节点的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 删除单链表倒数第n个节点

    基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...

  7. 13. 求链表倒数第k个节点

    题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...

  8. 链表倒数第n个节点

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

  9. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

随机推荐

  1. django的cookie和session以及内置信号、缓存

    cookie和session cookie和session的作用: cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话.两者最大的区别是cookie的信息是存放在浏览 ...

  2. Python MongoDB使用介绍

    MongoDB介绍 MongoDB是一个面向文档的,开源数据库程序,它平台无关.MongoDB像其他一些NoSQL数据库(但不是全部!)使用JSON结构的文档存储数据.这是使得数据非常灵活,不需要的S ...

  3. IndexedDB(本地存储)

    var students = [{ id: 1001, name: "Byron", age: 24 }, { id: 1002, name: "Frank", ...

  4. 解决nginx使用proxy_pass反向代理时,cookie丢失的问题

    1. 如果只是host.端口转换,则cookie不会丢失.例如:    location /project {        proxy_pass   http://127.0.0.1:8080/pr ...

  5. mysql数据库存储路径更改 数据文件位置

    使用了VPS一段时间之后发现磁盘空间快满了.本人的VPS在购买的时候买了500gb的磁盘,提供商赠送了20GB的高性能系统磁盘.这样系统就有两个磁盘空间了.在初次安装mysql 的时候将数据库目录安装 ...

  6. 使用Docker Mysql 5.7

    Mysql已经提供了Docker image,可以很方便开启一个mysql服务器.官方介绍了两种连接方式, 在其他App Docker 容器中通过--link访问Mysql服务端容器 启动另外一个My ...

  7. 简单PE类代码

    三个文件分别是类定义文件pefile.h;类实现文件pefile.cpp;类调用文件petype.cpp. #ifndef PE_FILE_H #define PE_FILE_H #include & ...

  8. SwipeMenuListView在ScrollView里上下滑动导致菜单不能显示完全的bug解决方法

    这是因为上下滑动的时候,事件被ScrollView截获了,这时候应该禁止ScrollView截获上下滑动事件,解决方法如下 public class NoRollSwipeMenuListView e ...

  9. JavaScript的==和===运算符

    JavaScript提供两个相等运算符:==和 ===.      简单说,它们的区别是相等运算符( ==)比较两个值是否相等,严格相等运算符( ===)比较它们是否为“同一个值”.如果两个值不是同一 ...

  10. latex公式编号

    1 \begin{flalign*} 2 % In this way (this arrange of &), the equation will in the center and alig ...