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. C# DataGridView改变行颜色无效问题

    有一个需求是DataGridView中不符合要求的行变为红色,这网上查到的代码是 Row.DefaultCellStyle.BackColor = Color.Red;即可 但是写完之后发现不起作用 ...

  2. spring-boot logback配置

    接着上篇的代码,日志在不同环境下的配置也不一样,所以要分开配置,主要使用maven的profile 1.1 在pom.xml中添加 <profiles> <profile> & ...

  3. pyothon学习笔记2-元组

    # 1.元组对象不可修改,元组中列表对象的对象可以修改 t = (1,2,[1,2,3]) t[2] = [1,2,3,4] # 'tuple' object does not support ite ...

  4. 关于 RESTful API 中 HTTP 状态码的定义

    最近正好使用了一会儿 Koa ,在这说一下自己对各个 请求码的见解和使用场景,懒人直接看 200.400.401.403.404.500 就可以了. 其中 2XX/3XX 其实都是请求成功,但是结果不 ...

  5. T-SQL语言基础(1)之理论背景

    从学校就开始接触和使用 SQL 了,但一直没有怎么细细去了解它,最近入职的公司比较重 T-SQL 部分,所以就准备系统的学习一下. 买了一本<Microsoft SQL Server 2008 ...

  6. JAVA项目中文件重命名方式

    可以直接改类名,之后eclipse会报错: 点击×号会给出解决方案: 注意:不能通过右击servlet文件直接重命名.否则运行之后程序会报错(文件路径不对之类的)

  7. linux----------centos下添加环境变量

    1.添加PHP的环境变量.如图操作 其中在 /etc/profile里面编辑的内容是:只加了这一行,箭头所指的那一行. 2.需要添加其他环境变量就在后面用 :追加 PATH=$PATH:/usr/lo ...

  8. java快排思想

    1分治思想 1.1比大小在分区 1.2从数组中取出一个数做基准数 1.3将比他小的数全放在他的左边,比他大的数全放在他的右边 1.4然后递归 左边 和右边 }

  9. Zepto源码分析之二(新旧版本zepto.Z方法的区别)

    在上一节中讲到Z()方法,是在初始化函数init中直接调用zepto.Z() zepto.Z = function(dom, selector) { dom = dom || [] dom.selec ...

  10. Python 入门知识捡漏

    一.对于变量的作用域,执行申明在内存中存在,该变量就可以在下面的代码中使用(即作用域) if  2==2: name = ‘tony’ print name 下面结论是否正确? 外层变量,可以被内层变 ...