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. Java_IO_文件的续写_小笔记

    package IO; import java.io.FileWriter; import java.io.IOException; class FileWrite_WenJianXuXie { /* ...

  2. goaccess iis w3c 自定义log 格式参考

    goaccess 支持强大的自定义log 格式,比如我们需要分析iis w3c 格式日志 参考iis w3c 字段 date time s-ip cs-method cs-uri-stem cs-ur ...

  3. zombodb安装试用

    pg 数据库安装 参考如下安装 yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pg ...

  4. LDAP解决多个服务器多个samba,不能指定多个samba域 的问题

    问题:在创建账号的时候,必须指定一个sambaDomain,但是只能指定一个,但是我有多个samba域要集成,那怎么办呢,怎么弄都只能登陆一个samba,不能所有的都登,经过反复的测试,反复的测试,找 ...

  5. bootstrap弹出模态框会给body加padding的解决方法

    bootstrap弹出模态框会给body加padding,导致页面缩放的解决方法: 在页面或是css文件里加上($paddingSize为less变量,需要改成像素或是其他单位,如12px,1rem) ...

  6. EF 指定字段修改

    public virtual void Modify(T model, params string[] ProNames) { DbEntityEntry entry = db.Entry<T& ...

  7. <ROS> 通讯方式之 Action

    Ros 官网介绍 http://wiki.ros.org/actionlib 一个简易的action教程,package -- example_action_server. action文件夹内存放 ...

  8. PCL中Sample_consensus分割支持的几何模型

    随机采样一致分割法,从点云中分割出线.面等几何元素 #include <pcl/sample_consensus/method_types.h> #include <pcl/samp ...

  9. windows下使用vscode编写运行以及调试C/C++

    未经允许,禁止转载,唯一出处:tangming博客园 最后更新于2019年4月4日: 多次更新,内容较多,篇幅较大,但如果是喜欢visual stdio code这款编辑器的话建议仔细阅读,有疑问的地 ...

  10. 【mybatis】之trim

    <trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...