Lintcode 166. 链表倒数第n个节点
-----------------------------------
最开始的想法是先计算出链表的长度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个节点的更多相关文章
- LintCode之链表倒数第n个节点
题目描述: 我的代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * L ...
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- 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个节点,保证链表中节点的最 ...
- [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 ...
- 删除单链表倒数第n个节点
基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...
- 13. 求链表倒数第k个节点
题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- [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 ...
随机推荐
- linux tomcat 启动
在tomcat bin目录下启动时报权限不够 在bin目录下输入:chmod u+x *.sh 可解决 查看tomcat 是否关闭 ps -ef|grep java 例如显示如下 说明还没关闭root ...
- ASP.NET MVC4 URL传递汉字参数不能正确接收
前两天写了个项目,在chrome上做的开发和测试. 拿给了产品,产品使用IE8.7.6进行测试的时候,发现很多报错.原因是URL里面的汉字在后台接收时显示的时乱码. 百度之~~ 最终解决方案, 在WE ...
- 《SQL必知必会》学习笔记(一)
这两天看了<SQL必知必会>第四版这本书,并照着书上做了不少实验,也对以前的概念有得新的认识,也发现以前自己有得地方理解错了.我采用的数据库是SQL Server2012.数据库中有一张比 ...
- UITextField的placeholder文字的位置,颜色等的自定义设置
//控制placeHolder的位置,左右缩20 -(CGRect)placeholderRectForBounds:(CGRect)bounds { CGRect inset = CGRectMak ...
- Java学习笔记15
do-while循环时while循环的变体语法如下:do{ // 循环体 语句(组);}while(循环继续条件); 如果循环中的语句至少需要执行一次,那么建议使用do-while循环. for循环 ...
- Mysql字符集设置
转 基本概念 • 字符(Character)是指人类语言中最小的表义符号.例如’A'.’B'等:• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(Encodi ...
- jQuey知识点三 解析json数据
1.解析简单数据 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="vie ...
- Linux(Ubuntu)下安装NodeJs
用以下命令来升级系统,并且安装一些Node.JS必要的包. Linux(Ubuntu)下安装NodeJs 安装nodeJS之前,如果没有安装g++ make libssl-dev等, 1.更新系统和依 ...
- ASP.NET上实现
ASP.NET上实现 fengzhuang.cs: using System;using System.Collections.Generic;using System.Linq;using Syst ...
- ASP.NET运作流程
当我们在浏览器输入域名访问服务器资源时,会向服务器发送Http请求,并经由IIS处理后,交由ASP.NET托管程序处理,进入ASP.NET管道.在IIS内部如何处理我们不需要深入去了解,在ASP.NE ...