删除链表中倒数第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)的更多相关文章

  1. 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 + ...

  2. 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 ...

  3. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  4. 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 ...

  5. 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 ...

  6. 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. ...

  7. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. AGC016D - XOR Replace 置换/轮换

    目录 题目链接 题解 代码 题目链接 AGC016D - XOR Replace 题解 可以发现一次操作相当于一次置换 对于每个a上的位置映射到b对应 可以找到置换群中的 所有轮换 一个k个元素的轮换 ...

  2. python3 excel文件的读与写

    from openpyxl import load_workbook class RwExcelFile: def read_Excel(self,file_path): ''' 读取excel中所有 ...

  3. MVC 程序在编译时提示 GAC与 Temporary ASP.NET Files目录内引用文件版本不一致

    今天在调试Mvc程序时,提示GAC与Temporary ASP.NET Files目录内引用文件版本不一致. [A]System.Web.WebPages.Razor.Configuration.Ho ...

  4. 关于Android文件Apk下载的那点事

    1.Android文件Apk下载变ZIP压缩包解决方案 如果你的下载服务器为Nginx服务器,那么,在Nginx安装目录下的conf/mime.types文件的对应位置,加上以下一行语句,指定APK文 ...

  5. 高性能平滑动画_requestAnimationFrame

    高性能平滑动画_requestAnimationFrame 在下一次重绘之前,执行一个函数

  6. apt下载open-jdk8报错add-apt-repository: command not found

    今天下载jdk8报错 在Ubuntu下,时不时会有这个错误的. add-apt-repository: command not found sudo apt-get install software- ...

  7. Myeclipse的使用技巧

    1.1.MyEclipse自定义注释   一.修改进入路径:  Window->Preference->Java->Code Style->Code Template-> ...

  8. 单调栈+前缀和 || Nowcoder || 牛客小白月赛13 || 小A的柱状图

    题面:小A的柱状图 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> #define l ...

  9. 2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践

    2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践 免杀原理及基础问题回答 实验内容 任务一:正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil- ...

  10. [archlinux]在linux使用aria2下载磁力链接

    1.在公网出口做好dnat,端口映射,虚拟服务等.导致内网主机的udp和tcp端口上. 2.在linux主机上,加两条防火墙规则 ┬─[tong@T7:~]─[:: PM] ╰─>$ sudo ...