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 ...
随机推荐
- webstorm 添加文件模板
Ctrl+Shift+A 搜索设置 File Teamplate 添加 File Teamplate
- CentOS 7 yum nginx MySQL PHP 简易环境搭建
用centos自带的yum源来安装nginx,mysql和php,超级方便,省去编译的麻烦,省去自己配置的麻烦,还能节省非常多的时间. 我们先把yum源换成国内的阿里云镜像源(当然不换也可以),先备份 ...
- CentOS设置服务开机启动的方法
CentOS设置服务开机启动的两种方法 1.利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后 ...
- (IIS8/8.5/Apache)301域名重定向
用Apache的.htaccess来做301域名转向1.开启apache支持.htaccess,方法:在Apache的配置文件httpd.conf中,找到<Directory /> ...
- CSS的IE6、IE7、FF兼容性写法
blue;< /td> Firefox 背景变蓝色 red /9; IE8 背景变红色 *black;< /td> IE7 背景变黑色 _background:orange; ...
- How to enables AX email functionality without Outlook
/***************************************************************** (C) Copyright DENTSPLY Internatio ...
- WPF之旅(三)- 布局之StackPanel
说到WPF的界面布局,相信很多朋友都写过Html代码.在WPF中,大多数程序都使用类似Web的(flow)流布局.在使用流布局模型时,各种控件可以按特定的要求来排列,在窗口内容发生变化时,比如窗口大小 ...
- C#中的interface、virtual和abstract
一.Abstract: abstract方法必须在abstarct类中声明,没有默认实现,子类必须实现. 二.Virtual: virtual方法可以声明在abstract类中,也可以声明在非abst ...
- 使用本地光盘安装Microsoft .NET Framework 3.5 for Win8.1/WinServer2012R2
.NET Framework 3.5 作为的SQL Server 2012的先决条件,假如使用图形化方式需要使用internet,对于服务器部署时缓慢的一点(需要下载后安装) 以下提供一个使用使用安装 ...
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...