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 ...
随机推荐
- pthread 的几个结构体
http://blog.csdn.net/yangzhongxuan/article/details/7397139 /* Copyright (C) 2002,2003,2004,2005,2006 ...
- xHTML与HTML的写法有什么不同?
全部标签都必须小写 在XHTML中,全部的标签都必须小写.不能大写和小写穿插当中.也不能全部都是大写. 事比例如以下. 错误:<Head></Head><Body> ...
- LA 3415 保守的老师
题目链接:https://vjudge.net/contest/161820#problem/E 题意: 有一些同学,要从中选出一些同学来,人数尽量多,但是,两两之间要满足至少一个条件(身高差> ...
- html5中event获取data和class
获取data和class var tare=$(e.relatedTarget).data("id");var tar=event.target;console.log(tare) ...
- SQL数据完整性
1.数据的完整性 1. 什么是数据的完整性 保证用户输入的数据保存到数据库中是正确的 2.添加数据完整性 在创建表的时候给表添加约束 3.完整性分类 实体完整性.域完整性.引用完整性 2.完整 ...
- Python—面向对象01
1.如何使用类 # 先定义类 class LuffyStudent(): school = "luffycity" # 数据属性 def learn(self): # 函数属性 p ...
- H5新增API和操作DOM
博客原文:https://dobinspark.com.cn/ H5-dom扩展 获取元素 document.getElementsByClassName ('class'); //通过类名获取元素, ...
- vue项目模拟后台数据
这次我们来模拟一些后台数据,然后去请求它并且将其渲染到界面上.关于项目的搭建鄙人斗胆向大家推荐我的一篇随笔<Vue开发环境搭建及热更新> 一.数据建立 我这里为了演示这个过程所以自己编写了 ...
- HTML5笔记——第一节
我的第一个网页 <!--html的基础模板--><!--html hyperText markup Language--><!--声明文档类型--><!DOC ...
- jdk下载安装
1.下载地址:https://www.oracle.com 注册,登陆,选择版本下载(注意,下载时提示you must accept the license agreement before down ...