Remove Nth Node From End of List 解答
Question
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Solution -- One pass
Use two pointers, slow and fast. Fast pointer firstly move n steps. Then, when fast pointer reaches end, slow pointer reaches the node before deleted node.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast = head, slow = head;
if (head == null)
return head;
while (n > 0) {
fast = fast.next;
n--;
}
// If remove the first node
if (fast == null) {
head = head.next;
return head;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
Remove Nth Node From End of List 解答的更多相关文章
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 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 + ...
随机推荐
- hdu3870-Catch the Theves(平面图最小割)
Problem Description A group of thieves is approaching a museum in the country of zjsxzy,now they are ...
- 【jquery插件】-网页下雪效果
又到了年底,从圣诞节开始,可以发现各大网站的活动是越来越多,都变的银装素裹,本人由于业务需求也需要做这么一个效果,所以就通过google大神找了一下相关资源,结果就出现了N种雪花效果的方式.种 ...
- Android 之 Eclipse 导入 Android 源码
很多人都下载过下图中的 Sources for Android SDK,但是很少人知道怎么用 下载完毕后可以再 Android SDK 根目录下看到 sources 文件夹内 有 andr ...
- xmind教程
xmind是什么东西我不多说.作为一个程序员,我通常用来编写一个文档.比如某个模块的设计或者流程图. 一开始我是以word画图的方式来用xmind的,即想要什么图形,就去插入里面找.结果碰了一鼻子灰, ...
- Visual Studio 2015 使用ODP.net进行EF开发
刚转了新公司,以前公司都是用VS+MSSQL作为开发工具的 现在新公司由于数据库是Oracle,而且新公司比较小规模,开发团队也没有什么规范 访问数据库的方式一直使用ADO.net的DataTable ...
- SpringMVC+easyUI CRUD 添加数据C
接一篇文章,今天上午实现了添加数据.以下是Jsp.里面主要是看newUser()和saveUser().注意这函数里的url,newUser()里面去掉url属性.还要注意的一个问题 <div ...
- Hook linux 网络封包
要注册一个hook函数需要用到nf_register_hook()或者nf_register_hooks()系统API和一个struct nf_hook_ops{}类型的结构体对象 一个简单的demo ...
- 自定义TypeConverter把基础类型转换为复杂类型
原文(http://tech.it168.com/d/2008-06-30/200806300953554_all.shtml) TypeConverter对于编写ASP.NET Server Con ...
- jquery渐隐轮播
html <body> <div id="banner"> <div id="banner_bg"></div> ...
- PHP学习笔记十五【面向对象二】
<?php class Cat{ //public 访问修饰符 public $name; public $age; } //创建 $cat1=new Cat; $cat1->name=& ...