题目:

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.

题解:

这道题也是经典题,利用的是faster和slower双指针来解决。

首先先让faster从起始点往后跑n步。

然后再让slower和faster一起跑,直到faster==null时候,slower所指向的node就是需要删除的节点。

注意,一般链表删除节点时候,需要维护一个prev指针,指向需要删除节点的上一个节点。

为了方便起见,当让slower和faster同时一起跑时,就不让 faster跑到null了,让他停在上一步,faster.next==null时候,这样slower就正好指向要删除节点的上一个节点,充当了prev指针。这样一来,就很容易做删除操作了。

slower.next = slower.next.next(类似于prev.next = prev.next.next)。

同时,这里还要注意对删除头结点的单独处理,要删除头结点时,没办法帮他维护prev节点,所以当发现要删除的是头结点时,直接让head = head.next并returnhead就够了。

代码如下:

 1    public static ListNode removeNthFromEnd(ListNode head, int n) {
 2         if(head == null || head.next == null)
 3             return null;
 4             
 5         ListNode faster = head;
 6         ListNode slower = head;
 7         
 8         for(int i = 0; i<n; i++)
 9             faster = faster.next;
             
         if(faster == null){
             head = head.next;
             return head;
         }
         
         while(faster.next != null){
             slower = slower.next;
             faster = faster.next;
         }
         
         slower.next = slower.next.next;
         return head;
         
         }

Remove Nth Node From End of List leetcode java的更多相关文章

  1. Remove Nth Node From End of List [LeetCode]

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

  2. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

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

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

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

  5. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  6. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  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. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  9. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

随机推荐

  1. 关于CSRF的那点事儿

    0x01 CSRF简介     CSRF,也称XSRF,即跨站请求伪造攻击,与XSS相似,但与XSS相比更难防范,是一种广泛存在于网站中的安全漏洞,经常与XSS一起配合攻击. 0x02 CSRF原理 ...

  2. 一个将PDF转word、图片、PPT的在线工具

    smallpdf 真的超级棒! https://smallpdf.com/cn

  3. Python并发编程系列之多线程

    1 引言 上一篇博文详细总结了Python进程的用法,这一篇博文来所以说Python中线程的用法.实际上,程序的运行都是以线程为基本单位的,每一个进程中都至少有一个线程(主线程),线程又可以创建子线程 ...

  4. Java 集合之 Map

    Map 就是另一个顶级接口了,总感觉 Map 是 Collection 的子接口呢.Map 主要用于表示那些含有映射关系的数据,存储的是一组一组的键值对.Map 是允许你将某些对象与其它一些对象关联起 ...

  5. Oceanus:美团HTTP流量定制化路由的实践

    背景简述 Oceanus是美团基础架构部研发的统一HTTP服务治理框架,基于Nginx和ngx_lua扩展,主要提供服务注册与发现.动态负载均衡.可视化管理.定制化路由.安全反扒.session ID ...

  6. Android IntentService

    IntentService简要分析 IntentService 继承自 android.app.Service.内部实现极其简单. 首先在 onCreate()中去开启了一个 HandlerThrea ...

  7. 1022 Digital Library (30)(30 point(s))

    problem A Digital Library contains millions of books, stored according to their titles, authors, key ...

  8. 20162318 2018-2019-2《网络对抗技术》Exp0 Kali安装 Week1

    1.配置虚拟机 参考博客链接 2.安装kali与配置网络 参考博客链接 3.配置共享文件夹 参考博客链接 4.更换软件源 参考博客链接

  9. LeetCode:删除排序数组中的重复项 (Remove Duplicates from Sorted Array)

    public class RemoveDuplicates { /** * 修改数组,使数组有序不重复.超出长度不考虑. * @param 排序数组 * @return 数组不重复数的个数 */ pu ...

  10. 解决请求参数的中文乱码问题(get、post)

    2018-11-28 在web请求与响应中,会遇到乱码问题,比如填写表单数据时,难免会输入中文,姓名.公司名称等.由于HTML设置了浏览器在传递请求参数时,采用的编码方式是UTF-8,但在解码时采用的 ...