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. sas share 备忘录

    options comamid=tcp;libname payable 'E:\shouen';proc server authenticate=optional id=share1 msgnumbe ...

  2. 2018北美部分CS项目学费

    yearly cost from official website USC 城里 24credit about 49k + cost of room&food BU 城里 NEU 65k 城里 ...

  3. Maven安装以及环境配置(Windows)

    Maven是什么? Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周期. 在 ...

  4. 删除n天前的所有目录和文件

    删除目录 find /your_dir/ -maxdepth -type d -mtime + -exec rm -rf {} \; 删除文件 find /目录/ -mtime + -name &qu ...

  5. Python开发之AJAX

    一.概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用:一个简单操作 ...

  6. springboot通过poi导出excel

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

  7. linux 下的常用操作命令

    一,Linux的介绍 略. 二,文件类的常用命令 三,权限类的常用命令

  8. PHP中json_encode()问题

    PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 要想不转义,在后面加个参数即可 json_encode($data, JSON_UNESCAPED_UNICODE);  ...

  9. 获取get请求后面的参数

    var str = "www.baidu.com?id=1&name=zhangsan"; var data = str.split("?"); con ...

  10. ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python)

    ROS学习手记 - 2.1: Create and Build ROS Package 生成包(Python) 时隔1年,再回来总结这个问题,因为它是ros+python开发中,太常用的一个操作,需要 ...