【数据结构】算法 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 ...
随机推荐
- 06-Python入门学习-元组、字典、集合类型
一.元组 一:基本使用:tuple 1 用途: 记录多个值,当多个值没有改的需求,此时用元组更合适 2 定义方式: 在()内用逗号分隔开多个任意类型的值 t=(1,1.3,'xx',('a','b') ...
- iframe ios中h5页面 样式变大
实际项目开发中,iframe在移动设备中使用问题还是很大的,说一说我的那些iframe坑 做过的这个后台管理框架,最开始的需求是PC,但随着业务需要,需要将项目兼容到ipad,后台的框架也是使用的开源 ...
- mac上adb command not found
第一种报错(使用的自带mac命令行) bash: adb: command not found 1.vim ~/.bash_profile ,如果.bash_profile不存在,先touch ~/. ...
- 简单的3d变换
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- git删除掉已经保存的用户名密码
以保存的用户名密码删除,先找到变量存在的位置: git config -l To help track down the setting, I'd try to use: git config --l ...
- Codechef July Challenge 2018 : Subway Ride
传送门 首先(想了很久之后)注意到一个性质:同一条边有多种颜色的话保留3种就可以了,这是因为假如最优解要求当前位置与相邻两条边都不相同,那么只要有3条边,就肯定可以满足这一点. 完事就做一个nlogn ...
- bootstrap_栅格系统_响应式工具_源码分析
-----------------------------------------------------------------------------margin 为负 使盒子重叠 等高 等高 ...
- 关于finally代码块是否一定被执行的问题
一般来说,只要执行了try语句,finally就会执行 但是,有以下几种情况需要特殊考虑 具体例子看链接 点击这里 第一点 try代码块没有被执行,意思就是错误在try代码块之前就发生了. 第二点 ...
- swust oj 971
统计利用先序遍历创建的二叉树的深度 10000(ms) 10000(kb) 3331 / 8436 利用先序递归遍历算法创建二叉树并计算该二叉树的深度.先序递归遍历建立二叉树的方法为:按照先序递归遍历 ...
- nginx,maven
nginx反向代理 负载均衡 keepalive高可用 lvs负载均衡算法 mvn build自定义命令 install安装到本地仓库