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 ...
随机推荐
- NO.008-2018.02.13《折桂令·春情》元代:徐再思
折桂令·春情_古诗文网 折桂令·春情 元代:徐再思 平生不会相思,才会相思,便害相思.生下来以后还不会相思,才刚刚懂了什么是相思,却深受着相思之苦. 身似浮云,心如飞絮,气若游丝.身像飘浮的云,心 ...
- Linux下elk安装配置
安装jdkJDK版本大于1.8 elk下载地址:https://www.elastic.co/products注意:elk三个版本都要保持一致. rpm -ivh elasticsearch-5.4. ...
- libevent将信号封装为socket通知的核心代码
#include"stdafx.h" #include"iostream" #include "algorithm" #include&qu ...
- spring 四种数据源配置方式
1.spring自带的数据源 DriverManagerDataSource XML代码: <bean id="dataSource" class="org.spr ...
- 【[ZJOI2010]网络扩容】
题目 第一问直接板子敲上去 第二问并不明白直接在残量网络上加边的神仙做法 非常显然我们需要让流量加\(k\),那么我们就使得网络里的总流量为\(maxf+k\),\(maxf\)是第一问求出来的最大流 ...
- WebStorm11 注册码及激活
网址:http://www.jianshu.com/p/5ce394a28ce5 1.获取注册码 打开第一个网址(IntelliJ IDEA 注册码),我们能看到下面的界面,直接点击获取激活码,将生成 ...
- 【洛谷P2324】[SCOI2005]骑士精神
骑士精神 题目链接 #include<iostream> #include<cstdio> using namespace std; int t,MAXD,sx,sy; ][] ...
- Tomcat+Oracle配置连接池的例子
我这有一个Tomcat+Oracle连接池的例子,放上来和大家分享一下. Tomcat +Oracle 连接池配置 Author: Kenneth.Leaf@GalaxySoft Date: / ...
- Struts2 第五讲 -- Struts2与Servlet的API解耦
为了避免与 Servlet API 耦合在一起, 方便 Action 做单元测试, Struts2 对 HttpServletRequest, HttpSession 和 ServletContext ...
- oracle 的分页查询与mysql 的分页查询
oracle 的分页查询: select * from (select o.*,rownum rn from Persons o) where rn >40 and rn <=60 : ...