lintcode :nth to Last Node In List 链表倒数第n个节点
题目:
链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n。
给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1.
解题:
某年408计算机考研题目
Java程序:
/**
* 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) {
// write your code here
if(head ==null)
return null;
if( head.next ==null && n==1)
return head;
ListNode p = head;
ListNode current = new ListNode(0);
current.next = head;
while( p.next!=null){
if(n>1){
p = p.next;
n --;
}else{
p = p.next;
current = current.next; }
}
return current.next;
}
}
总耗时: 2548 ms
Python程序:
lintcode :nth to Last Node In List 链表倒数第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 ...
- [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 ...
- [LeetCode] 19. 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 ...
- LintCode 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. 分析:设两个指针 p1和p2 ...
- 链表倒数第n个节点
找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...
- lintcode166 链表倒数第n个节点
链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...
- 删除单链表倒数第n个节点
基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...
- 13. 求链表倒数第k个节点
题目:输入1个链表,输出该链表倒数第k个节点,有头指针和尾指针.链表倒数第0个节点是链表的尾指针节点. 代码: /* 尾指针是倒数第0个,则倒数第k个是正数第len-k个,计算len */ #incl ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
随机推荐
- C# 汉字转拼音(转)
(一)将汉字转化成全拼代码: private void button1_Click(object sender, EventArgs e) { this.textBox2.Text = Hz2Py.C ...
- spring boot单元测试(转)
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性.凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...
- Delphi XE5教程10:Delphi字符集
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...
- GITHUB 提交错误 Error: Permission denied (publickey) 解决
1. 在开发机上生成自己的密钥 ssh-keygen -b 1024 -t rsa -b 指密钥对长度 -t 指加密方式 Enter file in which to save the key ( ...
- cppcheck使用
一.splint介绍 splint是一个动态检查C语言程序安全弱点和编写错误的程序.splint会进行多种常规检查,包括未使用的变量,类型不一致,使用未定义变量,无法执行的代码,忽略返回值,执行路径未 ...
- [WinForm]- 设置DataGridView单元格内根据不同值显示图片
首先设置要显示图片的列 DataGridViewImageColumn status = new DataGridViewImageColumn(); status.DisplayIndex = ; ...
- 在eclipse中使用jax-ws构建webservices服务端和客户端
服务端: package com.yinfu.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebServi ...
- 微信小程序、应用号、订阅号、服务号、企业号小总结
微信小程序是现在微信推出的一个新的项目,但是很多人都不是很清楚微信小程序是怎么一回事,不明白到底怎样分别微信小程序和别的公众号.订阅号等的区别,那么让小编来给你介绍一下. 微信小程序目前是内侧阶段,是 ...
- android 播放语音文件出现 prepare failed ,不能下载amr文件
amr文件的路径正确,但是android 却不能播放出来. 调试发现时根本就没有下载下来 原因: IIS服务器不允许下载该文件,需要配置MIME 解决方法: 进入IIS目录,配置MIME
- 关于拓扑排序(topologicalsort)
假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...