Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

Subscribe to see which companies asked this question

解答:

额这道题没想出什么好方法……感觉这样要两次遍历好烦啊……当然链表嘛就要注意头节点……有空再看看吧TAT

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode *tmp = head;
    ;

    while(NULL != tmp){
        count++;
        tmp = tmp->next;
    }
    if(count == n){
        return head->next;
    }
    tmp = head;
    ){
        count--;
        tmp = tmp->next;
    }
    tmp->next = tmp->next->next;
    return head;
}

LeetCode OJ 19. Remove Nth Node From End of List的更多相关文章

  1. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  2. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

  3. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

  4. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  5. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  6. 【LeetCode OJ】Remove Nth Node From End of List

    题目:Given a linked list, remove the nth node from the end of list and return its head. For example: G ...

  7. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  8. [Leetcode][Python]19: Remove Nth Node From End of List

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...

  9. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

随机推荐

  1. linux下的进程信息管理

  2. 支持向量机(SVM)算法分析——周志华的西瓜书学习

    1.线性可分 对于一个数据集: 如果存在一个超平面X能够将D中的正负样本精确地划分到S的两侧,超平面如下: 那么数据集D就是线性可分的,否则,不可分. w称为法向量,决定了超平面的方向:b为位移量,决 ...

  3. CentOS安装nginx以及负载均衡的搭建

    依赖环境,没有安装的需要安装一下 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install open ...

  4. for练习.html

    <script> 偶数 var str=""; for (var i = 1 ; i <= 100; i++){ if (i%2 == 0) { //str = ...

  5. Call to undefined function think\finfo_open()

    Fileinfo 扩展 - Linux面板- 宝塔面板 上传文件,出现错误 安装的fileinfo扩展

  6. angularjs的路由ui.router

      <!-- 引入路由插件 --> <script src="vendor/angular-ui-router/release/angular-ui-router.min. ...

  7. springboot通过poi导出excel

    Maven引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  8. WPF 异常其他信息: “对类型“BaseControl.KImgButton”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“38”,行位置为“22”。

    引发的异常:“System.Windows.Markup.XamlParseException”(位于 PresentationFramework.dll 中) 其他信息: “对类型“BaseCont ...

  9. java 多线程(生产者消费者)

    转 https://www.oschina.net/code/snippet_111708_25438 这个问题挺经典,我这个解法的本质在于将问题抽象为生产者消费者模型,但是是一个特殊的生产者消费者模 ...

  10. android 开发 实现多个动态权限的方法(并且兼容6.0以下的版本权限授权)

    android开发权限授权因为版本的不同有不同的授权方式,6.0以下的版本使用的是在注册表中添加权限的静态授权(这种授权权限提示只会出现在app安装的时候),而6.0以上(包含6.0)就需要动态授权的 ...