这题比较简单,方法有很多。其中一种比较有意思的做法是设置两个指针,一个先走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. BZOJ5321 & 洛谷4064 & LOJ2274:[JXOI2017]加法——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5321 https://www.luogu.org/problemnew/show/P4064 ht ...

  2. BZOJ1040:[ZJOI2008]骑士——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1040 题面大意:n个人有一个价值和一个最恨的人,现在组出一个队伍使得价值最大且没有仇恨关系. ——— ...

  3. LibreOJ #6220. sum(数论+构造)

    题目大意:在数组中找出一些数,使它们的和能被n整除 这题标签是数学,那我就标题就写数论好了... 显然如果数组中有n的倍数直接取就行. 那假设数组中没有n的倍数,把数组中的数求前缀和后全部%n,会得到 ...

  4. NOIP2018普及&提高题解

    第一次考$NOIP$的我已经自闭了 $CCF$告诉我们了一件事情,要对自己写的程序有信仰,相信$CCF$的数据是水的 普及组: 分数:$100+100+30+100=330$ $1.titile$: ...

  5. 理清一下JavaScript面向对象思路

    借这篇文章理清一下自己的思路,同时也希望能给和我同样一知半解的同学理清一下思路.引发思考来自于我犯的一个错误,错误代码是这样的: 1 var o = { 2     ... 3 } 4 var obj ...

  6. 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow

      Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Yes ...

  7. BAT script to set local account password never expire

    BAT script wmic UserAccount where Name="account123" set PasswordExpires=False

  8. jedis在线文档网址

    jedis在线文档网址:http://tool.oschina.net/apidocs/apidoc?api=jedis-2.1.0

  9. .NET中使用switch和java不一样的地方。

    1.不能这样贯穿 我们知道,java 和 C在使用switch时候可以这样. switch (i) { //java中此处不使用break // 执行了case 1:对应的语句后直接 贯穿到 case ...

  10. 大聊Python----json与pickle数据序列化

    用于序列化的两个模块 ☆json,用于字符串和python数据类型间进行转换 ☆pickle,用于python特有的类型和python的数据类型间进行转换 Json模块提供了四个功能:dumps.du ...