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.

思路:

利用快指针先走n步,找到倒数第n个节点

然后删除。

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* fast = head;
ListNode* slow = head; for(int i = ;i <= n-;++i) {
fast = fast->next;
}
// 一共有N个元素,n=N
if(fast==NULL) return head->next; while(fast->next!=NULL) {
fast = fast->next;
slow = slow->next;
}
ListNode* tmp = slow->next;
slow->next = slow->next->next;
tmp = NULL;
return head;
}
};

注意需要保存前缀

    public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast = head;
ListNode slow = head;
ListNode fakehead = new ListNode(0);
ListNode pre = fakehead;
fakehead.next= head;
for(int i = 0;i< n ;i++)
fast = fast.next;
while(fast != null){
pre = slow;
slow = slow.next;
fast = fast.next;
}
pre.next = slow.next;
return fakehead.next; }
}

19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)的更多相关文章

  1. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

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

  2. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  3. LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)

    题目链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/?tab=Description   Problem: 移除距离 ...

  4. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

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

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

  6. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

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

  8. 19. 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, ...

  9. [leetcode 19] Remove Nth Node From End of List

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

随机推荐

  1. Laravel5.1 搭建博客 --文章的增删改查

    教程源于:Laravel学院 继文件上传后呢,咱来搞一搞文章的事情. 1 更改数据表 我们需要改改数据表的结构 因为涉及到重命名列名 所以咱需要引入一个包:Doctrine: composer req ...

  2. Raw-OS源代码分析之idle任务

    分析的内核版本号截止到2014-04-15,基于1.05正式版,blogs会及时跟进最新版本号的内核开发进度,若源代码凝视出现"???"字样.则是未深究理解部分. Raw-OS官方 ...

  3. 剑指 offer set 21 圆圈中最后剩下的数字

    思路 1. 经典解法是用环形链表模拟圆圈, 然后每次减少一个节点. 时间复杂度为 o(mn), 空间复杂度为 o(n) 2. 转化成数学问题, 递推公式决定下一个元素. 时间复杂度为 o(n), 空间 ...

  4. videoview 播放视频

    -videoVIew 继承SurfaceView 使用android的VideoView来播放一个视频,步骤是: 1,在xml中创建一个videoView, 2,在java中导入,然后创建一个Medi ...

  5. Spring学习笔记--使用Spring基于Java的配置

    我们需要使用@Component注解来定义一个配置类,在配置类中我们定义Bean: package com.moonlit.myspring; import org.springframework.c ...

  6. [SharePoint 2010] 如何安裝啟用多國語系支援的SharePoint網站

    SharePoint 2010的多國語系支援與前一版的SharePoint 2007有所不同,SharePoint 2010內允許使用者自行決定使用介面語系的選擇. SharePoint 2010的多 ...

  7. MapRecude

    任务:分析通话记录,查处每个手机号码有哪些打过来的号码 13510921776 10086 13710148751 10086 13914248991 10086 13510921776 137101 ...

  8. eclipse中设置在编译运行项目之前自动保存修改的文件

    Window -> Preferences -> General -> Workspace -> “Save automatically before build” Windo ...

  9. iOS中navigationItem修改标题的颜色

    UIColor * color = [UIColor redColor];//这里我们设置的是颜色,NSDictionary * dict = [NSDictionary dictionaryWith ...

  10. HTTP Transaction Delays

    w客户端.服务器超载 HTTP The Definitive Guide 与建立TCP连接以及传输请求和相应报文的时间相比,事务处理的时间是很短的.除非客户端或服务器超载或正在处理复杂的动态资源,否则 ...