【数据结构】算法 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 ...
随机推荐
- Selenium + Python +CSV
绪论 首先写这个文章的时候仅仅花了2个晚上(我是菜鸟所以很慢),自己之前略懂selenium,但是不是很懂csv,这次相当于练手了. 第一章 环境介绍 具体实验环境 系统 Windows10教育版 1 ...
- 潭州课堂25班:Ph201805201 tornado 项目 第十一课 项目改进和优化(课堂笔记)
使用 Bootstrap 前端框架 可以在 bootCDN 这里找 jquery ,poppe.js 文件 当聊天室发来一第图片链接时,自动保存图片到服务器,并保存相关信息到数据库,系统向该用户发出 ...
- request请求携带证书,如:微信企业零钱付款
const Promise = require('bluebird') const request = Promise.promisifyAll(require('request')) const w ...
- C/C++中的内存对齐问题和pragma pack命令详解
这个内存对齐问题,居然影响到了sizeof(struct)的结果值.突然想到了之前写的一个API库里,有个API是向后台服务程序发送socket请求.其中的socket数据包是一个结构体.在发送soc ...
- Solve Error: "errcode": 48001, "errmsg": "api unauthorized hint"
当你想给微信公众号(不是测试账号)自定义菜单创建接口,遇到如下错误: OK Connection: keep-alive Date: Sat, 01 Dec 2018 05:02:08 GMT Con ...
- Linux_相关命令(学习,备忘)
1.Linux 查看实时cpu使用率: top 说明:top命令即时显示process的动态 2.查看cpu处理器使用率: cat /proc/stat 3.平均cpu使用率 4.赋予文件夹下所有文件 ...
- Linux下批量修改后缀名
1.用find和xargs添加后缀名 [root@node99 yum.repos.d]# ls -ltr total 32 -rw-r--r--. 1 root root 5701 Nov 23 2 ...
- urllib库的应用及简单爬虫的编写
1.urllib库基础 1.1爬虫的异常处理 常见状态码及含义 301 Moved Permanently:重定向到新的URL,永久性 302 Found:重定向到临时的URL,非永久性 304 No ...
- Git最牛最全详解
阅读目录 Git是什么 SVN与Git的最主要的区别 在windows上如何安装Git 如何操作 创建版本库 把文件添加到版本库中 版本回退 理解工作区与暂存 ...
- 2018-2019-2 网络对抗技术 20165336 Exp3 免杀原理与实践
2018-2019-2 网络对抗技术 20165336 Exp3 免杀原理与实践 1.基础问题回答 (1)杀软是如何检测出恶意代码的? 根据计算机病毒课程知道了每个病毒都有其对应的特征码,杀软是根据这 ...