Lintcode174-Remove Nth Node From End of List-Easy
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的更多相关文章
- 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 ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 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 ...
- 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 ...
- 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. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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 ...
- 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 + ...
- 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 ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
随机推荐
- oracle 主键,非空,检查,唯一,默认,外键约束
--首先添加主键约束alter table studentadd constraint PK_student_sno primary key(sno) --删除约束alter table studen ...
- 2019春第五周作业Compile Summarize
这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够精通关于数组内部运作原理 这个作业在哪个具体方面帮助我实现目标 如何输出一行的连续字符 参考文献与网址 ...
- python 不知道是啥
1.判断两个大文件是否是同一个文件 import os import hashlib import time start = time.time() path1 = r"E:\视频资料\el ...
- !!在js中的用法
var obj = ""; //undefinedconsole.log(!!obj); //false(强制转换成Boolean类型)
- jackson 流式API
http://www.cnblogs.com/lee0oo0/articles/2652528.html Jackson提供了三种可选的JSON处理方法 1.流式API com.fasterx ...
- 2018-2019-1 20189203《Linux内核原理与分析》第九周作业
第一部分 课本学习 进程的切换和系统的一般执行过程 进程调度的时机 Linux内核系统通过schedule函数实现进程调度,进程调度的时机就是内核调用schedule函数的时机.当内核即将返回用户空间 ...
- c# 有序链表合并 链表反转
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- leaflet.toolbar.js
leaflet.toolbar.js 参考:https://www.javascriptcn.com/read-38464.html
- Fiddler抓包【7】_次要功能和第三方插件
1.替换HTTP Request Host 应用场景:进行开发时,线上去测试跳转调试 替换命令:urlreplace news.baidu.com www.baidu.com: 清除命令:urlrep ...
- python 练习1(流控制)
#!/usr/bin/python #_*_ coding:utf-8 _*_ #练习题 #1.使用while循环输入 1 2 3 4 5 6 8 9 10 #a.定义一个变量存放数字 #b.用whi ...