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. 常用docker镜像

    oracle12c: mkdir -p /path/to/oradata docker run --name oracle12c \ -p 1521:1521 -p 5500:5500 \ -v /p ...

  2. ubuntu 16.04 静态ip的配置

    nssa-sensor1@nssa-sensor1:~$ vim /etc/network/interfaces 以下是编辑文件的内容# interfaces(5) file used by ifup ...

  3. spring得到所有Action类

    applicationContext对象.getBeansOfType(BaseAction.class)

  4. qt 软件打包

    今天呈现的客户端完成了要打包发布,想了一下还不会,就问了一下度娘,在此记录一下学习的程度 1>将QT编译工具的BUG模式切换成Release模式,在Release模式下生成一个*.exe的可执行 ...

  5. MySQL 可以用localhost 连接,但不能用IP连接的问题

    连接问题 由于数据库配置的问题,当我们用ip连接数据库时,会出现连接不上的情况,而用localhost连接则没有任何的问题.解决方法: mysql安装完后,默认是root用户,root用户只能在服务器 ...

  6. Shell流程控制(if,else,case,while,for,until)

    1.条件选择 1.1.if 语句 语法十分简单 #!/bin/bash MATH_SCORES="$1" NAME="$2" if [ -z "${M ...

  7. SAPCertifiedTechnologyAssociate-SystemAdministration(SAPHANAasaDatabase)

    C_TADM55_75 SAP Certified Technology Associate - System Administration (SAP HANA as a Database) with ...

  8. 【Social listening实操】作为一个合格的“增长黑客”,你还得重视外部数据的分析!

    本文转自知乎 作者:苏格兰折耳喵 ----------------------------------------------------- 在本文中,作者引出了"外部数据"这一概 ...

  9. 学习笔记:Vue

    https://cn.vuejs.org/v2/api/ 官网API https://cn.vuejs.org/v2/guide/ 官网教程 http://www.runoob.com/vue2/vu ...

  10. 关于时间:UTC/GMT/xST/ xDT

    UTC ,Coordinated Universal Time GMT ,Greenwich Mean Time GMT和UTC 一般来说是意义几近相同的, 不过 UTC Coordinated Un ...