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! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
随机推荐
- javascript 的引入
目录 一.静态引入 1. html标签script引入 2. esm 中import ModuleName from 'module/path' 3. commonjs 中 const ModuleN ...
- 《图解HTTP》读书笔记(一:网络基础TCP/IP)
好书什么时候开始读都不晚.作为一个测试人员,是一定要掌握一些网络的基础知识的.希望能够边读书边在这里记录笔记,便于加深理解以及日后查阅. 一.TCP/IP协议族 计算机与网络设备要互相通信,双方必须基 ...
- 【转】Spring Boot 构建应用——快速构建 Spring Boot 应用
Spring Boot 简化了 Spring 应用开发,不需要配置就能运行 Spring 应用,Spring Boot 的自动配置是通过 Spring 4.x 的条件注解 @Conditional 来 ...
- Cocos Creator 橡皮差(刮刮卡)功能(转)
实现一个刮刮卡的效果,于是在论坛里搜集了一些资料并且看了一下CCMask的源码,做出来一套可用的教程,分享给大家.(WEBGL和Native端测试可用) maskNode是详细设置如下 我们在 scr ...
- unittest报告出现dict() -> new empty dictionary错误解决办法
unittest报告出现dict() -> new empty dictionary错误解决办法 说一下原因,这是由于unittest中采用了ddt驱动. 由于版本问题导致 问题如图: 解决办 ...
- 《linux就该这么学》第十一节课: 第九章,网卡绑定与sshd服务
8.4.服务的访问控制列表 TCPwrappers是RHEL7中默认启用的流量监控程序,能够对服务做出允许或拒绝. TCPwrappers服务由两个文件控制: /etc/hosts.allow ...
- Android -- 《 最美有物》好看的点赞效果
1,前天在鸿洋的公众号上看到一款不错的点赞效果,是仿最美有物的点赞,再加上自己最近学习状态很差,自己想着通过这个效果练手一下,果然,花了整整两天的时间,按照以前的效率的话一天就够了,哎,已经调整了一个 ...
- 初识JDBC
1,概念: JDBC(Java DateBase Connective) ,即利用Java语言操作数据库语言 2,示例:Mysql中的JDBC 1,新建一个Dynamic Web Projectx项目 ...
- 已有的PHP安装gd扩展
第一步 安装依赖 1.安装xpm yum install libXpm-devel 2.安装zlib wget http://zlib.net/zlib-1.2.8.tar.gz tar -xzvf ...
- C#-----类DateTime的常用方法
1.TryParse(string s, out DateTime result) 将日期和时间的指定字符串表示形式转换为其 System.DateTime 等效项,并返回一个指示转换是否成功的 ...