Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
Notice
The minimum number of nodes in list is n.
Given linked list: 1->2->3->4->5->null, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5->null.
Can you do it without getting the length of the linked list?
分析:
为了删除倒数第n个node,我们需要找到那个node的parent,然后parent.next = parent.next.next. 这里我们创建一个dummy node用来处理删除的是第一个node的情况。
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode parent = dummy;
for (int i = ; i < n; i++) {
if (head == null) {
return null;
}
head = head.next;
}
while (head != null) {
head = head.next;
parent = parent.next;
}
parent.next = parent.next.next;
return dummy.next;
}
}
转载请注明出处:cnblogs.com/beiyeqingteng/
Remove Nth Node From End of List的更多相关文章
- 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、jQuery、AJAX、JSON 解释
- poj1703 并查集
输入是2个不在一起的人,可以用一个数组来保存和他矛盾的人.这样find的时候就find(hash[]);就可以: #include<stdio.h> #include<string. ...
- 常用的Java 架包(jar)的用途
前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 http://mvnrepository.com 查询 slf4j-api 简介:slf4j并不是一种具体的日志系统,而是一个用户 ...
- Hibernate-二级缓存策略
二级缓存的策略 当多个并发的事务同时访问持久化层的缓存中的相同数据时,会引起并发问题,必须采用必要的事务隔离措施. 在进程范围或集群范围的缓存,即第二级缓存,会出现并发问题.因此可以设定以下4种类型的 ...
- Java基础-gs(垃圾回收)
Java垃圾回收概况 Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代 ...
- href的参数含有中文在IE下乱码的解决
这是在使用kendo grid的自定义链接时遇到的一个坑,链接如下: var TempStr = "<a href='" + Url.Action("EditUse ...
- HDU 4901 The Romantic Hero
The Romantic Hero Time Limit: 3000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u D ...
- hihocoder 1154 Spring Outing
传送门 #1154 : Spring Outing 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 You class are planning for a spring ...
- 连通性2 无向图的割边 (cut edge)
这是DFS系列的第二篇 割边的概念 In graph theory, a bridge, isthmus, cut-edge, or cut arc is an edge of a graph who ...
- Beta版本——冲刺计划及安排
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...