题目:

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. 《JAVA软件结构与数据结构》第一周学习总结

    学号 20172326 <JAVA软件结构与数据结构>第一周学习总结 教材学习内容总结 软件质量的几大特性 增长函数与大O记法 大O记法用来表示表示增长函数,从而来表示算法的复杂度 算法的 ...

  2. JDK源码分析(五)——HashSet

    目录 HashSet概述 内部字段及构造方法 存储元素 删除元素 包含元素 总结 HashSet概述   从前面开始,已经分析过集合中的List和Map,今天来介绍另一种集合元素:Set.这是JDK对 ...

  3. 获取类的属性并排除特定属性(getType().GetProperties())

    当获取一个类型(class)的所有属性时,想排除指定属性,该如何操作? 比如:EF中一个实体类型UserEntity,通过反射获取这个类的属性时,想排除这个为映射的字段ID 使用以下方法即可! Pro ...

  4. XShell通过中转服务器直接连接目标服务器

    最近由于公司生产环境的变化,使得我们不能使用自己的机器连接到生产环境去,而是要通过跳板机中转才可以连接.于是今天尝试使用 XShell 通过跳板机直接转接到生产环境. 一.使用代理方式 首先填写连接信 ...

  5. [UOJ422]小Z的礼物

    设要取的物品集合为$S$,$E=n(m-1)+(n-1)m$,$x_T$为覆盖了$T$中至少一个元素的$1\times2$数量 $$\begin{aligned}\sum\limits_{i=1}^\ ...

  6. Apache之.htaccess备忘录(二)

    博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我. 转载请注明"深蓝的镰刀" 书接上回,<Apache之.htaccess备忘录(一)& ...

  7. tyvj:1038 忠诚 线段树

    tyvj:1038 忠诚 Time Limit: 1 Sec  Memory Limit: 131072KiBSubmit: 9619  Solved: 3287 题目连接 http://www.ty ...

  8. 探秘GO语言《比较C#与GO的性能》

    这段时间也来学学GO语言,听说它的性能相当的棒棒,我就拿C#来和它做比对一下. 这里只是单纯了做了for循环的比对,看看谁的循环快 C# 代码: static void Main(string[] a ...

  9. Unity3D中的UnitySendMessage方法的使用

    UnitySendMessage(“string”,“string”, ***),这是方法,我们至少需要传入两个参数,第一个参数为unity中的一个gameobject名称,第二个参数为这个gameo ...

  10. 拆解探索MagSafe电源接口结构和指示灯变颜色原理

    你有没有想过一个Mac的MagSafe接头里面有什么? 控制光线是什么? 在Mac如何知道它是什么样的充电器? 本文探讨的MagSafe连接器内,并回答这些问题. 2006年由苹果公司推出的MagSa ...