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. Object Slicing in C++

    In C++, a derived class object can be assigned to base class, but the other way is not possible. cla ...

  2. springboot整合mybatis之用外置服务器启动项目(二)

    在上一篇中我们是用的springboot自带的tomcat服务器,接下来想试一下 将springboot当做一个web项目 放在eclipse中用tomcat来启动. 首先在pom.xml中加上,移除 ...

  3. 01.Elasticsearch安装

    1.下载运行Elasticsearch 1.下载解压elasticsearch Elasticsearch官网地址:https://www.elastic.co/ Elasticsearch最新版下载 ...

  4. 使用Ansible自动配置Nginx服务

    1.首先安装好Ansible环境,具体步骤请见Ansible安装 2.先创建hosts文件(为后面编写脚本安装JDK做铺垫) [root@localhost /]# vi hosts [jdktest ...

  5. 转:JAVA.NET.SOCKETEXCEPTION: TOO MANY OPEN FILES解决方法

    最近随着网站访问量的提高把web服务器移到linux下了,在移服务器的第二天,tomcat频繁的报 java.net.SocketException: Too many open files错误,错误 ...

  6. 3.2 - FTP文件上传下载

    题目:开发一个支持多用户同时在线的FTP程序要求:1.用户加密认证2.允许同时多用户登录3.每个用户有自己的家目录,且只能访问自己的家目录4.对用户进行磁盘配额,每个用户的可用空间不同5.允许用户在f ...

  7. git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompatible or unsupported protocol version.”

    git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompat ...

  8. Python的subprocess模块(一)

    原文连接:http://www.cnblogs.com/wang-yc/p/5624880.html 一.简介 subprocess最早在2.4版本引入.用来生成子进程,并可以通过管道连接他们的输入/ ...

  9. 【JVM】线上应用故障排查

    高CPU占用 一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. 根据top命令,发现PID为28555的Java进程占用CPU高达200%,出现故障. 通过ps aux ...

  10. golang 系统包自动填写插件

    Make sure $GOPATH/bin is in your $PATH (Windows: %GOPATH%\bin goes in your %PATH%). [保证你的golang环境正常] ...