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. 在python项目中导出项目依赖的模块信息

    1.安装pipreqs pip install pipreqs 2.导出requriements.txt文件 在windows中,终端切换到项目所在的文件夹下: 运行: pipreqs ./ 如果遇到 ...

  2. Django中cookie和session使用

    cookie和session的简单使用 def cookie(request): """ 操作cookie """ resp = HttpR ...

  3. Angular7上手体验

    准备工具 Node.js Angular requires Node.js version 8.x or 10.x. 查看当前你的node版本可以在CMD中输入 node -v npm -v 开发工具 ...

  4. REDHAT YUM本地源的搭建和使用

    yum源一般分为两种,本地yum源和本地网络yum源,前者是通过文件提供安装包,后者是通过网络下载安装包: 由于Redhat7.3的yum源需要注册付费,所以往往会出现下载yum源安装包失败,如下图: ...

  5. javascript中计算两个时间日期间隔的天数

    <script>              /*                  计算两个日期的时间间隔天数              */              //时间字符串的格 ...

  6. 2.第一个python 程序

    第一个python程序 一..python程序的编写步骤 1.创建  xxx.py文件(文件名不要中文) 文件名要以py为扩展名,因为导入的时候其他扩展名会报错.如果不导入的情况可以不限制扩展名. 2 ...

  7. asp.net 去掉小数点后面多余的0

    很多时候,比如gridview内,不想现实从数据库带出的多余小数 ,比如 4.01000. 那就可以做一个函数: /// <summary> /// 去掉小数点后多余的0, 0本身显示为0 ...

  8. Windows7 IIS 出现错误,并非所有的功能被成功更改

    (1)进入UAC,将其调至最低. 具体操作方法:点击[开始],在程序搜索中输入UAC,回车后则进入UAC控制界面. UAC,用户帐户控制设置如下,这个设置主要是避免Windows7种由于用户帐户权限控 ...

  9. MAVEN简介之——pom.xml

    maven构建的生命周期 maven是围绕着构建生命周期这个核心概念为基础的.maven里有3个内嵌的构建生命周期,default,clean和site. default是处理你项目部署的:clean ...

  10. 修改AD FS

    https://technet.microsoft.com/en-us/windows-server-docs/identity/ad-fs/operations/ad-fs-user-sign-in ...