【数据结构】算法 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 ...
随机推荐
- Linux中Hadoop的环境搭建
一:下载安装 Hadoop 1.1:下载指定的Hadoop 1.2:通过XFTP把文件上传到master电脑bigData目录下 1.3:解压hadoop压缩文件 tar -xvf hadoop-2. ...
- Python 小知识 杂七杂八 随手记
1.assert 断言语句 例1: print ‘11111111111’ assert 1==2 print ‘22222222’ 如果没有 assert 程序会输出 ‘1111111111 ...
- 红帽 Red Hat Linux相关产品iso镜像下载【百度云】【更新7.2】
RedHat Enterprise Server 6.7 for i386 Boot Disk:rhel-server-6.7-i386-boot.iso SHA-256 Checksum: 798d ...
- Docker ElK安装部署使用教程
一.简介 1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引 ...
- js如何判断数字是否有小数
//如果是5.00之类的,转换后,应该不要小数点后的位数 let num = 5.34; //let num = 5.00; let arr = num .toString().split(" ...
- 解决:vue项目中多个echarts图表只有最后一个随浏览器变化改变大小
window.onresize = () => { this.initChart()} 改为 window.addEventListener('resize', () => { this. ...
- delphi 自动获取串口
delphi 自动获取串口 https://blog.csdn.net/Nevermore_anger/article/details/79012875 版权声明:本文为博主原创文章,未经博 ...
- pkg-config 详解
转载自:https://blog.csdn.net/newchenxf/article/details/51750239 1 什么是pkg-config pkg-config是一个linux下的命令, ...
- .net中使用 道格拉斯-普特 抽希轨迹点
Douglas一Peukcer算法由D.Douglas和T.Peueker于1973年提出,简称D一P算法,是目前公认的线状要素化简经典算法.现有的线化简算法中,有相当一部分都是在该算法基础上进行改进 ...
- rpc轻量级框架实例