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.

题解:

  快慢指针。

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if (!head)
return head;
ListNode dummy(-);
dummy.next = head;
ListNode* fast = &dummy, *slow = &dummy; while (n-- >= && fast) fast = fast->next; while(fast) {
fast = fast->next;
slow = slow->next;
} ListNode* tmp = slow->next;
slow->next = slow->next->next;
delete tmp; return dummy.next;
}
};

【LeetCode】019. Remove Nth Node From End of List的更多相关文章

  1. 【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 ...

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

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

  3. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...

  4. 【一天一道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 ...

  5. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  6. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  7. 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 ...

  8. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

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

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

随机推荐

  1. Pairs of Integers

    Pairs of Integers You are to find all pairs of integers such that their sum is equal to the given in ...

  2. Eclipse 查看第三方jar包文件源代码解决方法

    1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方jar 包,如Spring等相关的依赖包时,并没有附加下载相应的源文件,所以经常出现如图的这种问题. ...

  3. java高级主题

    1 java.util.concurrent.locks.LockSupport park:阻塞线程. unpark:解除阻塞线程. 线程阻塞最基础的组件. 2 sun.misc.Unsafe 可以用 ...

  4. 洛谷2704 [NOI2001]炮兵阵地

    题目戳这里 Solution 状压DP很好的入门题,用熟练位运算貌似也没那么难. 首先分析一下题目: 看见n=100,m=10,立马就想到了状压,看起来也像DP,所以我们还是采用行号为阶段的状压DP. ...

  5. 7 Javascript:表单与验证-非空验证

    表单提交前要检查数据的合法性 在要对表单里的数据进行验证的时候,能够利用getElementById()来訪问网页上不论什么一个元素 每一个表单域都有一个form对象,可被传给不论什么验证表单数据的函 ...

  6. samsung n143 brightness on linux mint

    sudo vi /etc/default/grub Find the line which says GRUB_CMDLINE_LINUX="" enter acpi_backli ...

  7. inline 元素的特性

    http://www.maxdesign.com.au/articles/inline/ http://www.w3.org/TR/CSS2/visuren.html#inline-boxes htt ...

  8. php使用mysql数据库时中文不显示或显示异常解决方法

    如果出现标题所述问题,需从四个层面解决问题 1 项目文件是否为utf8编码 右键项目文件->properties->Resource->Text file encoding-> ...

  9. 20145229吴姗珊 《Java程序设计》第3周学习总结

    20145229吴姗珊 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 4.1类与对象 1.对象(Object):存在的具体实体,具有明确的状态和行为 2.类(Cl ...

  10. Python 案例一(计算人体体脂率)

    #计算人体体脂率 #输入部分 #身高 personHeight = input("请输入你的身高(m):") personHeight = float(personHeight) ...