这题比较简单,方法有很多。其中一种比较有意思的做法是设置两个指针,一个先走n步,然后再一起走。一个到了末尾,另一个也就确定了要删除元素的位置。

ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode dummy{-, head};
ListNode *p = &dummy, *q = &dummy;
for (int i = ; i < n; i++) // q 先走 n 步
q = q->next;
while(q->next) { // 一起走
p = p->next;
q = q->next;
}
ListNode *tmp = p->next;
p->next = p->next->next;
delete tmp;
return dummy.next;
}

leetcode 之Remove Nth Node From End of List(19)的更多相关文章

  1. LeetCode 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

  2. 【leetcode】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 ...

  3. 【leetcode】Remove Nth Node From End of List(easy)

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

  4. 【JAVA、C++】LeetCode 019 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, Give ...

  5. LeetCode(46)-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, ...

  6. [LeetCode 题解]: 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, Give ...

  7. leetcode 【 Remove Nth Node From End of List 】 python 实现

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

  8. leetcode 题解 || Remove Nth Node From End of List 问题

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

  9. [Leetcode][019] Remove Nth Node From End of List (Java)

    题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...

  10. 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

随机推荐

  1. harbor1.4.0高可用部署

    一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Parent 构造函数成为 Children 的方法,然 ...

  2. BZOJ3197:[SDOI2013]刺客信条——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3197 故事发生在1486 年的意大利,Ezio 原本只是一个文艺复兴时期的贵族,后来因为家族成员受 ...

  3. UVA.1584 环状序列

    UVA.1584 环状序列 点我看题面 题意分析 给出你一段换装DNA序列,然后让你输出这段环状序列的字典序最小的序列情况. 字典序字面意思上理解就是按照字典编排的序列,其实也可以理解为按照ASCII ...

  4. 解决无法安装cnpm,cnpm卡顿问题

    # 注册模块镜像 npm set registry https://registry.npm.taobao.org # node-gyp 编译依赖的 node 源码镜像 npm set disturl ...

  5. win7无法登陆linux samba共享

    网上查了一下资料,总共有以下几种做法: 1.防火墙 2. Open the Run command and type "secpol.msc". Press "conti ...

  6. Sublime Text 配置python文件

    每次配置Sublime Text的都是好一顿搜索配置信息,今天特地把自己电脑上配置好的信息保存下来,方便以后使用. 用到了 AutoPEP8.Anaconda.SublimeCodeIntel.Sub ...

  7. [LeetCode] 数组的最长连续数, O(n)解法

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. [EXT JS]"hasMany" association on ExtJS 4.1.1a

    ExtJS uses "hasMany" association to support nested json. However the sencha docs lacks wel ...

  9. java 反射和new的区别

    关于java 反射和new的区别,在这里我不做多讲,因为网上有大把资料,描述得很详细. 今天我只讲一点,为什么要用反射?直接用new不行么?干嘛弄得那么麻烦! 1.基本上效果差不多,但是new对象,无 ...

  10. Fiddler进行模拟POST、PUT提交数据注意点

    1.请求头要加 Accept: application/xml Content-Type: application/json 2.地址栏url地址后不要忘记加“/” 3.POST和PUT的对象参数都是 ...