LeetCode:19. Remove Nth Node From End of List(Medium)
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)的更多相关文章
- 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 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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 ...
- 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, ...
- 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 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【一天一道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 ...
- 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 ...
- 蜗牛慢慢爬 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 ...
随机推荐
- Ubuntu adb device
在ubuntu上经常出现系统无法识别android手机的情况,要解决这个问题可以用以下方法: 1. 执行 "android update adb", 这会创建"~/.an ...
- netbackup 8.1安装前注意事项
一.NetBackup 主服务器的 Web 服务器用户/组设置步骤 文章 ID:100034818 上次发布时间:2017-08-24 产品:NetBackup 问题 从 NetBackup 8. ...
- java 线程状态图
- SpringMVC学习记录七——sjon数据交互和拦截器
21 json数据交互 21.1 为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservi ...
- javascript对字符串的常见操作trim,ltrim,rtrim,isEmpty,isFloat等
1.验证字符串是否为空格.是否包含非法字符. //验证是否字符串有非法字符 function v_invalide_char(value,msg){ var arr = ['#','@','!','$ ...
- Validform 基于表单验证
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【题解】洛谷P2421[NOI2002]荒岛野人 (Exgcd)
洛谷P2421:https://www.luogu.org/problemnew/show/P2421 思路 从洞的最大编号开始增大枚举答案 对于每一个枚举的ans要满足Ci+k*Pi≡Cj+k*Pj ...
- JVM构架、GC垃圾回收机制的理解
JVM是Java Virtual Machine(Java虚拟机)的缩写 1.程序计数器 它的作用可以看做是当前线程所执行的字节码的行号指示器. 每个线程都有一个程序计算器,就是一个指针,指向方法区 ...
- 使用xlsx把json对象导出excel
1,首先使用npm下载xlsx.执行命令 npm install xlsx --save import { Component, OnInit } from '@angular/core'; //im ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...