1. 原题链接

https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/

2. 题目要求

给出一个链表,请删除倒数第n个结点并返回头节点

注意:给出的n总在合法范围内;只用一次遍历

3. 解题思路

删除倒数第n个结点,正着数即删除从表头结点开始的第L-n+1个结点。创建一个表头结点来指向头结点。

思路一:使用两次遍历。第一次遍历得到链表的长度,第二遍历删除第L-n+1个结点。

思路二:使用一次遍历。使用两个指针first和second,开始时first和second都指向头结点head。first指针先到达正数第n个结点,second指针不动。然后两个指针同步向后移动,保持两个指针之间的gap为n。当first.next==null时,second指针指向倒数第n个结点。

4. 代码实现

  1. package com.huiAlex;
  2.  
  3. import java.util.List;
  4.  
  5. /**
  6. * Definition for singly-linked list.
  7. * public class ListNode {
  8. * int val;
  9. * ListNode next;
  10. * ListNode(int x) { val = x; }
  11. * }
  12. */
  13. public class RemoveNthNodeFromEndofList19 {
  14. public static void main(String[] args) {
  15. ListNode l1 = new ListNode(1);
  16. ListNode l2= new ListNode(2);
  17. ListNode l3 = new ListNode(3);
  18. ListNode l4= new ListNode(4);
  19. ListNode l5 = new ListNode(5);
  20. ListNode l6= new ListNode(6);
  21. l1.next=l2;
  22. l2.next =l3;
  23. l3.next=l4;
  24. l4.next=l5;
  25. l5.next=l6;
  26.  
  27. ListNode ls = RemoveNthNodeFromEndofList19.removeNthFromEnd(l1,3);
  28. ListNode ls2 = l1.next.next.next;
  29. System.out.println("头结点:"+ls.val); // Expected:1
  30. System.out.println("删除nth结点后,其前驱结点的后继结点"+ls2.val); // Expected:5
  31.  
  32. }
  33. // 思路二代码实现
  34. public static ListNode removeNthFromEnd(ListNode head, int n){
  35. ListNode headPointer = new ListNode(0);
  36. headPointer.next = head;
  37. ListNode first = head,second = head;
  38.  
  39. for(int i =0;i<n+1;i++){ // first指针到达din个结点
  40. first=first.next;
  41. }
  42.  
  43. while(first!=null){ // 保持gap为n,两个指针同步后移
  44. first=first.next;
  45. second=second.next;
  46. }
  47. second.next=second.next.next; //删除倒数第n个结点
  48.  
  49. return headPointer.next;
  50.  
  51. }
  52.  
  53. // 思路一代码实现
  54. public static ListNode removeNthFromEnd2(ListNode head, int n) {
  55. ListNode headPointer = new ListNode(0);
  56. headPointer.next = head;
  57. ListNode first = head;
  58. int length = 0;
  59. while (first != null) { // 第一次遍历得到链表的长度
  60. length++;
  61. first = first.next;
  62. }
  63. length -= n; // 倒数第n个结点前面所有结点的长度
  64. first = headPointer;
  65. while (length > 0) { // 第二次遍历找到倒数第n个结点
  66. length--;
  67. first = first.next;
  68. }
  69. first.next = first.next.next; // 删除倒数第n个结点
  70. return headPointer.next;
  71. }
  72.  
  73. public static class ListNode {
  74. int val;
  75. ListNode next;
  76.  
  77. ListNode(int x) {
  78. val = x;
  79. }
  80. }
  81.  
  82. }

  

LeetCode:19. Remove Nth Node From End of List(Medium)的更多相关文章

  1. leetcode 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, Give ...

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

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

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

  4. LeetCode题解:(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, ...

  5. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

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

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

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

  8. LeetCode OJ 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, Give ...

  9. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

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

随机推荐

  1. pthread 的几个结构体

    http://blog.csdn.net/yangzhongxuan/article/details/7397139 /* Copyright (C) 2002,2003,2004,2005,2006 ...

  2. xHTML与HTML的写法有什么不同?

    全部标签都必须小写 在XHTML中,全部的标签都必须小写.不能大写和小写穿插当中.也不能全部都是大写. 事比例如以下. 错误:<Head></Head><Body> ...

  3. LA 3415 保守的老师

    题目链接:https://vjudge.net/contest/161820#problem/E 题意: 有一些同学,要从中选出一些同学来,人数尽量多,但是,两两之间要满足至少一个条件(身高差> ...

  4. html5中event获取data和class

    获取data和class var tare=$(e.relatedTarget).data("id");var tar=event.target;console.log(tare) ...

  5. SQL数据完整性

    1.数据的完整性 1. 什么是数据的完整性 保证用户输入的数据保存到数据库中是正确的 2.添加数据完整性 在创建表的时候给表添加约束 3.完整性分类 实体完整性.域完整性.引用完整性 ​ ​ 2.完整 ...

  6. Python—面向对象01

    1.如何使用类 # 先定义类 class LuffyStudent(): school = "luffycity" # 数据属性 def learn(self): # 函数属性 p ...

  7. H5新增API和操作DOM

    博客原文:https://dobinspark.com.cn/ H5-dom扩展 获取元素 document.getElementsByClassName ('class'); //通过类名获取元素, ...

  8. vue项目模拟后台数据

    这次我们来模拟一些后台数据,然后去请求它并且将其渲染到界面上.关于项目的搭建鄙人斗胆向大家推荐我的一篇随笔<Vue开发环境搭建及热更新> 一.数据建立 我这里为了演示这个过程所以自己编写了 ...

  9. HTML5笔记——第一节

    我的第一个网页 <!--html的基础模板--><!--html hyperText markup Language--><!--声明文档类型--><!DOC ...

  10. jdk下载安装

    1.下载地址:https://www.oracle.com 注册,登陆,选择版本下载(注意,下载时提示you must accept the license agreement before down ...