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.
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的更多相关文章
- 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 ...
- lintcode :nth to Last Node In List 链表倒数第n个节点
题目: 链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. ...
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- 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 ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
随机推荐
- ASP.NET MVC实现POST方式的Redirect
我们知道,在ASP.NET MVC中,要从一个Action跳转到另一个Action,通常是用一系列以“Redirect”开头的方法 Redirect RedirectToAction Redirect ...
- 在Windows和UNIX下利用PHP和LDAP进行身份验证
我现在的老板曾要求我为企业内部的Web服务提供一种标准的身份验证方法.我遇到的一个主要问题就是我们公司主要使用了两种平台:UNIX和.所以,我的第一个想法并不很成功:它要求每个员工都使用UNIX或者L ...
- 自动化测试UI Test, Performance Test, Load Test 总结整理
MSDN: 测试应用程序,Test apps early and often ,Improve Code Quality 推荐书: <Visual Studio 2015高级编程> < ...
- Java 使用正则表达式
用正则表达式来处理掉内容中的特定字符,下面的代码为,去掉P标签中的属性width 设置.将P标签处理后在拼接成字符串 /** * 给 P 标签去掉width 样式设置 * @param content ...
- 11.Android之常用对话框AlertDialog学习
(1)首先我们写个简单的AlertDialog对话框,要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,然后创建对话框可以设置对话框的属性,比如 ...
- 基于SVD的推荐算法
首先每行减去每列的均值,然后svd分解,得到USV,然后US代表用户矩阵u,SV代表项目矩阵v,那么预测评分为用户均值加上uv. 降维方法扩展性好,不过降维导致信息损失,而且与数据及相关,高维情况下效 ...
- 以一个权限系统来告别WebForm —开篇
前言: 当今是互联网的时代,我们己经阻止不了它的发展了,只有跟上脚步,才不会被抛弃,松散了这么久,该紧紧了. 背景: 我之所以说以一个权限应用系统来告别我的WebForm内部系统的生涯,是缘于我自 ...
- UDP 内网穿透 心跳
参考:http://blog.csdn.net/jacman/article/details/ 1: 启动一个Server. 2: 启动两个Client. 然后从Server端的Console里边可以 ...
- 轻量级应用开发之(08)UITableView
一 UITableView基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UISc ...
- jquery------提供灵活的方法参数
index.jsp <h1 >再次重逢的世界</h1> my.js $(document).ready(function(){ (function($){ $.fn.shado ...