Given a linked list, remove the n-th node from the end of list and return its head.

Example:

Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

Given n will always be valid.

Follow up:

Could you do this in one pass?

题意: 删除链表倒数第N个节点

Solution1: Two Pointers(fast and slow)

1. Let fast pointer to move n steps in advance, making sure there is n steps gap between fast and slow

2. Move fast and slow pointer together until fast.next == null

code

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/ /*
Time: O(n)
Space: O(1)
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy, fast = dummy; for (int i = 0; i < n; i++) // fast先走n步
fast = fast.next; while(fast.next != null) { // fast和slow一起走
slow = slow.next;
fast = fast.next;
}
//直接skip要删除的节点
slow.next = slow.next.next; // 思考为何不能写成 slow.next = fast;
return dummy.next;
}
}

[leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点的更多相关文章

  1. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  2. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  3. 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 ...

  4. 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

  5. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  7. Java [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 ...

  8. 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 ...

  9. (链表 双指针) leetcode 19. Remove Nth Node From End of List

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

随机推荐

  1. Excel转datatable

    如果想支持 .xls,.xlsx 两种格式 则必须安装一个exe文件,下载路径https://www.microsoft.com/zh-CN/download/details.aspx?id=1325 ...

  2. java 原子性 可见性 有序性

    原子性 原子性是指一个操作或多个操作要么全部执行完成且执行过程不被中断,要么就不执行. 如向变量x赋值操作 x = 10 是原子性的,就不会出现赋值操作进行到一半(x的低16位赋值成功,高16位没有赋 ...

  3. Java面试题 Web+EJB & Spring+数据结构& 算法&计算机基础

    六.Web 部分:(共题:基础40 道,基础37 道,中等难度3 道) 122.说出Servlet 的生命周期,并说出Servlet 和CGI 的区别? [基础] 答:Web 容器加载Servlet ...

  4. NumPy-快速处理数据--ndarray对象--数组的创建和存取

    本文摘自<用Python做科学计算>,版权归原作者所有. NumPy为Python提供了快速的多维数组处理的能力,而SciPy则在NumPy基础上添加了众多的科学计算所需的各种工具包,有了 ...

  5. git项目提交后执行添加忽略操作

    需要删除文件暂存区中的忽略文件 git rm -r --cached 需要忽略的已提交文件或文件夹 eg: git rm -r --cached target/

  6. Mysql 数据库操作之DDL、DML、DQL语句操作

    Mysql 数据库操作之DDL.DML.DQL语句操作 设置数据库用户名密码 l  Show databases 查看数据库列表信息 l  查看数据库中的数据表信息 ,格式: use 数据库名: sh ...

  7. string formating字符串格式化,function函数,group组,recursion递归,练习

    # -*- coding: UTF-8 -*- msg = 'i am {} my hobby is {}'.format('lhf',18) print(msg) msg1 = 'i am %s m ...

  8. PTA上Java问题自查与提问方法

    自查 首先请一定先看这篇文章<PTA中提交Java程序的一些套路>.该文囊括了PTA中提交Java程序的几乎所有常见问题,请仔细阅读可以少踩很多坑 题目测试方法:复制样例输入,然后打开一个 ...

  9. ArrayList add方法(转)

    由于 BrowerList 输出结果都是最后一条记录,后来网上查到了 if (dRead.HasRows) { List<Class_RejectQuery> BrowerList = n ...

  10. windows本地eclispe运行linux上hadoop的maperduce程序

    继续上一篇博文:hadoop集群的搭建 1.将linux节点上的hadoop安装包从linux上下载下来(你也可以从网上直接下载压缩包,解压后放到自己电脑上) 我的地址是: 2.配置环境变量: HAD ...