【数据结构】算法 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 ...
随机推荐
- [R] R语言for循环机制
在做数据分段截取的时候,发现for循环的表现和其他语言不太一样. 上代码: :) { i = i + print(i) } 结果: [] [] [] [] 即作为循环计次的i, 并不会因为在循环体中的 ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习5
#include <iostream>using namespace std;const MAXSIZE=12;const year=3;int main(){ char *month[M ...
- mysql学习1
1.什么是数据库? 数据的仓库,如在ATM的示例中创建了一个db目录,称其为数据库 2.安装 下载 http://dev.mysql.com/downloads/mysql/ 安装 windows: ...
- js一些代码
1判断金额正则 var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/; var money ...
- 利用PIL和Selenium实现页面元素截图
预备 照张相片 selenium.webdriver可以实现对显示页面的截图: from selenium import webdriver dr = webdriver.Firefox() dr.g ...
- __x__(34)0908第五天__ 定位 position
position 定位 指将原始摆放到页面的任意位置. 继承性:no 默认值:static 没有定位,原始出现在正常的文档流中 可选值: static : 默认值,元素没有开启定位 ...
- cmd 命令 net start mongodb 启动不了,提示 net 不是内部命令或者外部命令
1.要管理员的身份进入 cmd 2.右击我的电脑-->属性-->高级系统设置 3.选择高级-->环境变量 4.找到系统变量-->Path-->编辑 5.把 C:\wind ...
- Python基础之列表深浅复制和列表推导式
一.列表深浅复制: 浅拷贝内存图如下: 深拷贝内存图如下: 二.列表推导式: 实例: """ 列表推导式 练习:exercise01 """ ...
- python中的包与模块
'''模块与模块之间的调用''' import first #调用整个变量 print(first.Index) # #调用函数 print(first.hello()) # per = first. ...
- 前台js根据当前时间生成订单号
*********前台显示框**************** <input type="text" id="WIDout_trade_no" name=& ...