19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)
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.
Note:
Given n will always be valid.
Try to do this in one pass.
思路:
利用快指针先走n步,找到倒数第n个节点
然后删除。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* fast = head;
ListNode* slow = head; for(int i = ;i <= n-;++i) {
fast = fast->next;
}
// 一共有N个元素,n=N
if(fast==NULL) return head->next; while(fast->next!=NULL) {
fast = fast->next;
slow = slow->next;
}
ListNode* tmp = slow->next;
slow->next = slow->next->next;
tmp = NULL;
return head;
}
};
注意需要保存前缀
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast = head;
ListNode slow = head;
ListNode fakehead = new ListNode(0);
ListNode pre = fakehead;
fakehead.next= head;
for(int i = 0;i< n ;i++)
fast = fast.next;
while(fast != null){
pre = slow;
slow = slow.next;
fast = fast.next;
}
pre.next = slow.next;
return fakehead.next; }
}
19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)的更多相关文章
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
题目链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/?tab=Description Problem: 移除距离 ...
- 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 ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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 ...
- 19. Remove Nth Node From End of List
题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
随机推荐
- hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 配置使用TargetFrameworks输出多版本类库
1.类库右键 2.修改配置 修改前: <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <Targe ...
- 使用ProcDump工具抓取dump
首先得到要抓取的进程号 cd %windir%\syswow64\inetsrvappcmd list wp得到pid之后, 在任务管理器里发现w3wp.exe的CPU总在49%-60%左右, 间歇性 ...
- ArcGIS 相同要素类的多Shp文件或多要素合并
- 编译OSG_FBX插件
安装FBX的SDK,例如C:\Program Files\Autodesk\FBX\FBX SDK\2017.1\ 在CMake配置lib库路径的时候,使用 C:\Program Files\Auto ...
- IOS7开发~新UI学起(一)
本文转载至:http://blog.csdn.net/lizhongfu2013/article/details/9124893 IOS7在UI方面发生了很大改变,所以感觉有必要重新审视的学习一下(新 ...
- 关东升的《从零开始学Swift》3月9日已经上架
大家一直期盼的<从零开始学Swift>于3月9日已经上架,它是关东升老师历时8个月的呕心沥血所编著,全书600多页,此本书基于Swift 2.x,通过大量案例全面介绍苹果平台的应用开发.全 ...
- ajax请求步骤
ajax步骤:第一步:创建xmlhttprequest对象,var xmlhttp = new XMLHttpRequest(); XMLHttpRequest对象和服务器交换数据.第二步:使用xml ...
- Bootstrap学习记录
中文官网 Bootstrap 插件 Bootstrap Multiselect bootstrap-multiselect 的简单使用,样式修改,动态创建option JS组件系列——Bootstra ...
- Servlet------>mvc模式原理图
常用开发模式: 客户在客户端 访问,发送请求到servlet servlet调用service接口 service实现类调用dao接口 dao接口通过jdbc技术操作数据库,并存储到javabean, ...