题目:

链表倒数第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个节点的更多相关文章

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

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

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

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

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

  5. 链表倒数第n个节点

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

  6. lintcode166 链表倒数第n个节点

    链表倒数第n个节点 找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末 ...

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

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

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

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

  9. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

随机推荐

  1. 最初程序员的思维“修炼”之四——Android平台开发的“强制关闭”解决思路

    我和我的朋友参加一个比赛——物联网应用技能大赛,这个大赛所要求的技能有,硬件技术,Android平台开发技术,.NET平台开发技术,所以这是一个团队合作的比赛,基本上没有人能同时掌握这三种技术(在校生 ...

  2. 实现Server.UrlEncode和Server.UrlDecode功能的js代码

    <script> var EncodeURI = function(unzipStr,isCusEncode){    if(isCusEncode){        var zipArr ...

  3. Microsoft server software support for Microsoft Azure virtual machines

    http://support.microsoft.com/kb/2721672/en-us  Article ID: 2721672 - Last Review: November 22, 2014 ...

  4. 不逃离WIndows,Asp.Net就只能写写进销存管理系统

    那个丹麦人把C#语言弄的再好,但是如果程序放进Windows+IIS这个容器,就有问题了. 我在I5+8G内存的机子上测试Windows2008+IIs,直接ab 2000并发压默认的HTML主页,还 ...

  5. Linux 配置本地yum源

    Linux 配置无网络状态利用yum安装软件 在有网络的情况下安装软件只需一条yum install xxx命令,例如安装gcc只需一条指令:yum install gcc  那么在没有网络的情况下该 ...

  6. backup site collection

    http://stackoverflow.com/questions/5376380/sharepoint-2010-change-sitecollection-urlstsadm -o backup ...

  7. JPA学习---第六节:大数据字段映射与字段延迟加载

    1.大数据字段所需的注解 @Lob ,例如: @Lobprivate String info; 在mysql中映射产生的字段的类型是longtext:在oracle中是  CLOB @Lobpriva ...

  8. 关于 iOS 刷新效果实现的思路 和 mac软件网址推荐

    有一次面试,突然有个人问了我一个问题:MJRefresh的原理是什么? 我说这种效果可以有两种方法实现: 1.  UIRefreshControl 2.  通过监听scrollview的偏移量,自定义 ...

  9. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  10. [原创] zabbix学习之旅六:如何解决zabbix server在内网,而邮件发送服务器在外网的问题

    通过前面的文章,你已经可以快速地搭建一个报警系统,并能正常的收到报警邮件了.不过在很多企业级环境下,邮件发送服务器往往放在外网,而zabbix server放置在内网,在这种情况下,zabbix的报警 ...