链表倒数第n个节点

找到单链表倒数第n个节点,保证链表中节点的最少数量为n。

思路:设置两个指针first,second指向head,first指针先向前走n,然后两个指针一起走,first指针走到末尾,second走到倒数第n个指针!

代码

 /**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/ class Solution {
public:
/*
* @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 (NULL == head || n < ) return NULL;
ListNode *first = head;
ListNode *second = head;
while (n) {
first = first->next;
n--;
}
while (first) {
first = first->next;
second = second->next;
}
return second;
}
};

lintcode166 链表倒数第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. 删除单链表倒数第n个节点

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

  3. lintcode :nth to Last Node In List 链表倒数第n个节点

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

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

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

  5. 链表倒数第n个节点

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

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

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

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

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

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

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

随机推荐

  1. C#发送邮件类库

    public class Email { #region 发送邮件 /// <summary> /// 发送邮件 /// </summary> /// <param na ...

  2. STM32F103 ucLinux开发之四(内核启动后的调试)

    Stm32-uclinux启动后的调试 1.  修改__pfn_to_page使得能够启动 根据STM32F103 ucLinux开发之三(内核启动后不正常)的描述,内核无法启动是选择了平板内存模式后 ...

  3. win7利用winSCP上传文件到ubuntu server

    1.为ubuntu server设置root密码: sudo passwd root 先设密码在登录 2. su root进入root账户: 3.安装SSH:sudo apt-get install ...

  4. Office365学习笔记—创建WikiPage

    1,项目有个需求:项目表每更新一次,就把跟该项目有关的任务创建一个静态页(历史版本功能)! 注意事项:需要在页面上拖一个ContentEditer!将代码放在ContentEditer里面,因为我试过 ...

  5. 用javascript编写猜拳游戏(函数)

    const readline = require('readline-sync')//引用readline-sync console.log('欢迎进入猜拳游戏'); //电脑随机出拳 let fn ...

  6. 若是将Map作为Key,存入Redis,该如何操作?

    1.先封装HashMap Map<String,Object> map=new HashMap<String,Object>(); map.put("name&quo ...

  7. Swift_协议

    Swift_协议 点击查看源码 //协议 @objc protocol SomeProtocol:class { //class代表只用类才能实现这个协议 func test() //@objc:OC ...

  8. 替代Xshell的良心国产软件 FinalShell

    今年8月份NetSarang公司旗下软件家族的官方版本被爆被植入后门着实让我们常用的Xshell,Xftp等工具火了一把,很长时间都是在用Xshell,不过最近发现了一款同类产品FinalShell, ...

  9. ORA-00911: 无效字符 问题和解决

    1.原本java插入数据库表数据写法是这样的 String sql = "INSERT INTO AAA (id1,id2,id3,id4) VALUES ('1','2','3','4') ...

  10. QQ好友的价值玩法 及如何搞到几万好友?

    我们知道,现在的自媒体平台太多了.微信公众号,企鹅媒体平台,今日头条.搜狐.UC.一点等等等. 但是现在的话最主要的就是盈利,我们很多朋友玩自媒体这个在很多平台都有自己的账号和大量的粉丝.但是,最后大 ...