【数据结构】算法 LinkList (Remove Nth Node From End of List)
删除链表中倒数第n个节点
时间复杂度要控制在O(n)
Solution:
设置2个指针,一个用于确定删除节点的位置,一个用于计算倒数间距n。移动时保持2个指针同时移动。
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null) return null;
ListNode pre = head;
ListNode cur = head;
for(int i=0;i<n;++i){
cur = cur.next;
if(cur==null)
{
return head.next;
}
}
while(cur.next!=null){
cur=cur.next;
pre= pre.next;
}
pre.next = pre.next.next;
return head;
}
【数据结构】算法 LinkList (Remove Nth Node From End of List)的更多相关文章
- 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 + ...
- 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 题目整理-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 ...
随机推荐
- 探索JavaScript中Null和Undefined的深渊
当讨论JavaScript中的原始数据类型时,大多数人都知道的基本知识,从String,Number到Boolean.这些原始类型相当简单,行为符合常识.但是,本文将更多聚焦独特的原始数据类型Null ...
- [BZOJ2457][BeiJing2011]双端队列 (单调性)
正如lyd所说,和数据结构本身没什么太大关联 题意 中文题面 Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. ...
- TopCoder Div2
代码附在文末. 多组数据一定要初始化啊啊啊 贪心要[大胆]猜想,小心证明 A 题目翻译 题目描述 有两个正整数A和B,两个操作+3或者-2. 问,至少多少次操作可以让A变到B 输入 多组数据,第一行一 ...
- 小甲鱼Python第十八讲课后习题
笔记: 1.函数与过程:过程(procedure)是简单的,特殊且没有返回值的:函数(Function)有返回值 Python严格来说只有函数没有过程 2.局部变量:在局部生效如在函数中定义的变量 3 ...
- word使用新技能
office2013版,菜单栏-审阅-修订-所有标记,可以显示编辑过程中的所有修改步骤,还可查看未修改的原始状态.前提是“修订”按钮 要点亮! 给文档添加索引,并自动生成索引列表 文件-选项-显示-隐 ...
- HTML5_音视频标签 <audio> 和 <video>
HTML5_音视频标签 <audio> 和 <video> audio 和 video 都是 inline行内元素 如果浏览器支持,则不显示标签文本 IE8 不支持 audio ...
- ArcGIS AddIn 批量设置栅格图层背景色为透明
protected override void OnClick() { // // TODO: Sample code showing how to access button host // Arc ...
- RegExp正则3
1.正则:检索字符串的一条规则. 2.正则就是由元字符和修饰符构成的 3.写在//里面的都叫元字符,与元字符两种 一种是有特殊意义,一种没有特殊意义,没有特殊意义的就是字符本身. 特殊意思的元字 ...
- dtFindNearestPolyQuery :: process
dtFindNearestPolyQuery :: process(const dtMeshTile* tile, dtPoly** polys, dtPolyRef* refs, int count ...
- F#周报2019年第14期
新闻 发布F# 4.6 SAFE Stack v1.0 发布fable编译器2.2,Fable.Core 3及其它 发布ML.NET 1.0 RC Saturn:增加路由诊断页面 Visual Stu ...