题目:

链表倒数第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. nginx作反向代理,实现负载均衡

    nginx作反向代理,实现负载均衡按正常的方法安装好 ngixn,方法可参考http://www.cnblogs.com/lin3615/p/4376224.html其中作了反向代理的服务器的配置如下 ...

  2. Winform webBrowser 不跳转网页

    private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...

  3. laravel--为什么属性在模型中没有定义,却取出来了值,这些属性哪里来的

    看laravel模型中的这段代码, public function getLimitUsersAttribute() { return $this->user_limit - $this-> ...

  4. WPF:简洁为美

    (1)3行代码实现水印TextBox(Watermark  TextBox) 效果图: 源代码: <Grid> <Grid.Resources> <BooleanToVi ...

  5. Java String.split()注意点

    //String[] aa = "aaa|bbb|ccc".split("|");//错误 String[] aa = "aaa|bbb|ccc&qu ...

  6. innobackupex:Error:xtrabackup child process has died at /usr/bin/innobackupex

    使用innobackupex进行数据库备份,报如下错误:innobackupex --compress --parallel=4  --user=root  --password=yoon /expo ...

  7. Python标准库 urllib2 的使用

    1.Proxy 的设置 urllib2 默认会使用环境变量 http_proxy 来设置 HTTP Proxy. 如果想在程序中明确控制 Proxy,而不受环境变量的影响,可以使用下面的方式 impo ...

  8. 编辑器未包含main类型解决方法

    将文件移到 src 这个 Java Source Folder 下面去,现在在外面的 java 文件不会被当成一个需要编译的类,eclipse 不会编译 Java Source Folder 外面的任 ...

  9. iOS 进阶 第十天(0410)

    0410 在tableViewCell之间添加一根线,通栏 iOS应用数据存储的常用方式 plist存储文件 plist读取文件 下面是plist存储读取的图解: 注意:plist只能存储常见的属性. ...

  10. Notes of the scrum meeting(10/31)

    meeting time:3:00~4:30p.m.,October 30th,2013 meeting place:绿园 attendees: 顾育豪                        ...