174. Remove Nth Node From End of List

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

Example

Example 1:
Input: list = 1->2->3->4->5->null, n = 2
Output: 1->2->3->5->null Example 2:
Input: list = 5->4->3->2->1->null, n = 2
Output: 5->4->3->1->null

Challenge

Can you do it without getting the length of the linked list?

Notice

The minimum number of nodes in list is n.

双指针法:

定义快慢指针,先同时指向dummy结点。快指针(head)先比慢指针(preDelete)多走n步。

然后,快慢指针一起走,当快指针指向链表最后一个结点时,慢指针指向就是要删除结点的前一个结点。

ps: 对单向链表而言,删除结点时,必须操作要删除结点的前一个结点,而不是要删除的结点本身,否则无法使要删除结点的前一个和后一个结点连接。这也是这个题中,慢指针指向要删除结点的前一个结点,也因此推出快指针的临界位置。

代码:

public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
head = dummy;
ListNode preDelete = dummy; for (int i = 1; i <= n; i++) {
head = head.next;
}
while (head.next != null) {
head = head.next;
preDelete = preDelete.next;
}
preDelete.next = preDelete.next.next;
return dummy.next;
}

考虑特殊情况(n <= 0, head == null)

public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if (n <= 0) {
return null;
} ListNode dummy = new ListNode(0);
dummy.next = head; ListNode preDelete = dummy;
for (int i = 0; i < n; i++) {
if (head == null) {
return null;
}
head = head.next;
}
while (head != null) {
head = head.next;
preDelete = preDelete.next;
}
preDelete.next = preDelete.next.next;
return dummy.next;
}
}

Lintcode174-Remove Nth Node From End of List-Easy的更多相关文章

  1. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  2. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  3. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

  4. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

  5. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  6. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

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

  8. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  9. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  10. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

随机推荐

  1. html5网页录音

    demo https://xiangyuecn.github.io/Recorder/

  2. linux awk 常见字符串处理

    awk指定输出列: awk '{print $0} file' #打印所有列awk '{print $1}' file #打印第一列 awk '{print $1, $3}' file #打印第一和第 ...

  3. 2019-oo-第一次总结

    一.度量分析程序结构 1.UML类图分析 1.1第一次作业         1.2第二次作业 1.3第三次作业   1.4总结 从UML类图三次作业的可以看出,我从一个类到逐渐利用多个类,代码结构在不 ...

  4. CentOS配置apache多站点设置

    配置文件目录: /etc/httpd/conf.d /etc/httpd/conf/httpd.conf 错误日志文件在哪里? 网站文件目录: /var/www/html (Ubuntu/Centos ...

  5. 运维自动化之系统部署 PXE(二)

    PXE介绍 Preboot Excution Environment 预启动执行环境 Intel公司研发 基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持 ...

  6. 北京大学Cousera学习笔记--5-计算导论与C语言基础--计算机的基本原理-设计程序

    只要你认真的思考,你就会发现这个世界是如此的简单,正如我们想象的一样,正因为如此,我们的思考才更加的有价值 1.单词:关键字(有特定含义的):其他词用关键字定义出来 2.数和计算符号:数据类型+运算符 ...

  7. 《ASP.NET Core In Action》读书笔记系列五 ASP.NET Core 解决方案结构解析1

    创建好项目后,解决方案资源管理器窗口里我们看到,增加了不少文件夹及文件,如下图所示: 在解决方案文件夹中,找到项目文件夹,该文件夹又包含五个子文件夹 -Models.Controllers.Views ...

  8. webdriver.chrome()禁止加载图片

    from selenium import webdriver chrome_options = webdriver.ChromeOptions() prefs = {"profile.man ...

  9. wpf treeview 数据绑定 递归绑定节点

    1.先上效果 将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点. 1.xaml文件,将以下代码加入界面合适位置 <StackPanel&g ...

  10. Log4j2 日志级别

    Log4j2日志级别 级别 在log4j2中, 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < ...