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. 代码实现

package com.huiAlex;

import java.util.List;

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class RemoveNthNodeFromEndofList19 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2= new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4= new ListNode(4);
ListNode l5 = new ListNode(5);
ListNode l6= new ListNode(6);
l1.next=l2;
l2.next =l3;
l3.next=l4;
l4.next=l5;
l5.next=l6; ListNode ls = RemoveNthNodeFromEndofList19.removeNthFromEnd(l1,3);
ListNode ls2 = l1.next.next.next;
System.out.println("头结点:"+ls.val); // Expected:1
System.out.println("删除nth结点后,其前驱结点的后继结点"+ls2.val); // Expected:5 }
// 思路二代码实现
public static ListNode removeNthFromEnd(ListNode head, int n){
ListNode headPointer = new ListNode(0);
headPointer.next = head;
ListNode first = head,second = head; for(int i =0;i<n+1;i++){ // first指针到达din个结点
first=first.next;
} while(first!=null){ // 保持gap为n,两个指针同步后移
first=first.next;
second=second.next;
}
second.next=second.next.next; //删除倒数第n个结点 return headPointer.next; } // 思路一代码实现
public static ListNode removeNthFromEnd2(ListNode head, int n) {
ListNode headPointer = new ListNode(0);
headPointer.next = head;
ListNode first = head;
int length = 0;
while (first != null) { // 第一次遍历得到链表的长度
length++;
first = first.next;
}
length -= n; // 倒数第n个结点前面所有结点的长度
first = headPointer;
while (length > 0) { // 第二次遍历找到倒数第n个结点
length--;
first = first.next;
}
first.next = first.next.next; // 删除倒数第n个结点
return headPointer.next;
} public static class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
} }

  

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. http中COOKIE和SESSION有什么区别?(转知乎)

    作者:知乎用户链接:https://www.zhihu.com/question/19786827/answer/28752144来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  2. Android(java)学习笔记42:Map集合的获取功能

    1. Map集合的获取功能:  package cn.itcast_01; import java.util.Collection; import java.util.HashMap; import ...

  3. 【luogu P4711 「化学」相对分子质量】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4711 要细心模拟 #include <cstdio> #include <algorith ...

  4. C# 程序启动其他进程程序

    1  启动一个独立进程,需要用到的命名空间是:using System.Diagnostics;   进程类是 Process ,进程的相关参数信息类是 ProcessStartInfo 2  等待启 ...

  5. linux网络相关配制及命令

    1.虚拟机配制 查看ip: ip addr 配制网卡(读者可以忽略): 编辑虚拟网络编辑器,修改子网IP 查看ip,输入ip addr 开启网络:ifup eth0  关闭网络:ifdown eth0 ...

  6. 关于刷新同级layer弹框的解决方法

    在项目中遇到这种情况: 父页面点击详情,layer.open一个子页面A,子页面里面又存在操作按钮,点击使用parent.layer.open在打开一个子页面B,子页面B点击提交操作成功要刷新子页面A ...

  7. iOS之蓝牙开发—CoreBluetooth详解

    CoreBluetooth的API是基于BLE4.0的标准的.这个框架涵盖了BLE标准的所有细节.仅仅只有新的iOS设备和Mac是和BLE标准兼容.在CoreBluetooth框架中,有两个主要的角色 ...

  8. 爬虫——Handler处理器 和 自定义Opener

    我们之前一直都在使用的urlopen,这是一个特殊的opener(也就是模块帮我们构建好的). 但是基本的urlopen()方法不支持代理.cookie等其他的HTTP/HTTPS高级功能.所以要支持 ...

  9. 用 jQuery 实现表单验证(转载)

    jQuery 官方 API 地址: http://api.jquery.com/ 在线引用 jQuery:http://code.jquery.com/ ——选自<锋利的jQuery>(第 ...

  10. 转:java中的定时任务

    引自:http://www.cnblogs.com/wenbronk/p/6433178.html java中的定时任务, 使用java实现有3种方式: 1, 使用普通thread实现 @Test p ...