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. NO.007-2018.02.12《白头吟》两汉:卓文君

    白头吟_古诗文网_解析_鉴赏_赏析 白头吟 两汉:卓文君 白头吟:乐府<楚调曲>调名.据<西京杂记>卷三载,蜀地巨商卓王孙的女儿卓文君,聪明美丽,有文采,通音乐.孀居在家时,与 ...

  2. c++互斥锁的实现

    class IMyLock { public: virtual ~IMyLock(){} ; ; }; class Mutex : public IMyLock { public: Mutex(); ...

  3. 行高 line-height

    一.行高的定义 line-height(行高):两行文字基线之间的距离 1.什么是基线? 2.为何是基线? 3.需要两行吗? 1.什么是基线? 我们上学的时候都用过,抄写英文字母的时候.其中有一条红线 ...

  4. maven之构建多模块maven工程

    (一)环境搭建 1.Maven下载   ;   http://maven.apache.org/download.cgi 第一个在Linux使用,第二个是在Windows,第三和第四是源码: 我们将下 ...

  5. 去除a标签的下划线

    a:link ,a:visited,a:hover,a:active { text-decoration: none;}

  6. Ubuntu 16.04 安装 IDEA

    1.下载地址:https://www.jetbrains.com/idea/download/#section=linux 选择without jdk版本下载 2.下载完成 解压 到 /opt下 先却 ...

  7. 【洛谷P1196】[NOI2002]银河英雄传说

    银河英雄传说 题目链接 并查集时记录下以i为首的队列的长度(如果存在这个队列)num[i],便于合并, 和点i到队首的距离front[i],便于查询(在find时维护) #include<ios ...

  8. JS JavaScript事件循环机制

    区分进程和线程 进程是cpu资源分配的最小单位(系统会给它分配内存) 不同的进程之间是可以同学的,如管道.FIFO(命名管道).消息队列 一个进程里有单个或多个线程 浏览器是多进程的,因为系统给它的进 ...

  9. react(三):容器组件和傻瓜组件

    让一个组件只专注于一件事,如果发现让一个组件做的事情太多,就可以把这个组件拆分成多个组件让每一个组件只专注于一件事 <深入浅出react和redux> ---程墨 一个react组件最基本 ...

  10. Struts2知识点小结(三)--值栈与ognl表达式

    1.问题一 : 什么是值栈 ValueStack        回顾web阶段 数据交互问题?        客户端提交数据  到  服务器端    request接受数据+BeanUtils实体封装 ...