42-Remove Nth Node From End of List
- Remove Nth Node From End of List My Submissions QuestionEditorial Solution
Total Accepted: 106592 Total Submissions: 361392 Difficulty: Easy
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Submission Details
207 / 207 test cases passed.
Status: Accepted
Runtime: 8 ms
思路:利用快慢指针,快指针先走n步,然后快指针到末尾,那么慢指针走到第n个位置的前一个位置
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *pre=head,*p=head;
for(int i=0;i<n;++i){ //先向前走n步
if(p!=NULL)p=p->next;
else return head;
}
if(p==NULL)return head->next; //第n+1个位置为空,那么倒数第n个位置为首元素
while(p->next){
pre = pre->next; //快慢指针同时走,直到快指针到达最后一个节点
p = p->next;
}
pre->next = pre->next->next;//删除该节点
return head;
}
};
42-Remove Nth Node From End of List的更多相关文章
- 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解题报告—— 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 + ...
- 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 ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
随机推荐
- [对对子队]会议记录5.19(Scrum Meeting6)
今天已完成的工作 吴昭邦 工作内容:搭建第9关 相关issue:搭建关卡7.8.9 相关签入:feat: 第9关能够通过 何瑞 工作内容:搭建第9关 相关issue:搭建关卡7.8 ...
- DDL_Killer Alpha版本 Bug集中反馈处
本博客用于DDL_Killer Alpha版本的Bug集中反馈. 您可以在本博客的下方评论区处留言,反馈您在使用DDl_Killer的过程中遇到的问题,以帮助我们更好的改进本产品. 我们会尽快修复找到 ...
- mysqld_exporter监控mysql信息
mysqld_exporter监控mysql信息 一.背景 二.prometheus接入mysqld_exporter 1.安装mysqld_exporter 2.创建mysqld_exporter用 ...
- Noip模拟8 2021.6.17
T1 星际旅行 仔细一看,发现像一个欧拉路(简称一笔画). 满足"可以一笔画"的条件是: 1.所有点都有偶数条连边; 2.有偶数个点连奇数条边; 满足以上两个条件的任意一个即可一笔 ...
- 【学习笔记】Vizing 定理
图染色问题的经典结论 定义 称一个边染色方案合法当且仅当每个顶点连出的所有边的颜色都互不相同,如果此时出现了 \(k\) 个颜色那么称该方案是图的一组 \(k\) 染色 一张无向图的边着色数为最小的 ...
- 设计的MOS管三极管简单开关电路驱动能力不够3
16楼说得非常明白,补充一点,R3如果不要,会有下冲产生.4 Q: Z/ G G1 s8 Z- } 能解释下为什么会产生过冲吗?9 i, P* D* X) u. t/ b ^ 让我们这些菜鸟学习学 ...
- [LGP1866]编号
传送门 题意:找n个数,使得 $ 1 \leq a_i \leq Maxnumber_i $ 求有多少种组合 这题我们可以看到,还有一种无解的情况 我们可以先判断无解的情况 首先把Maxnumber数 ...
- wpa_supplicant启动出错rfkill: Cannot open RFKILL control device
在板子是调试网络,千辛万苦把wpa_supplicant及其依赖都移植编译进来了,在板子上调试启动的时候启动报错了 D/wpa_supplicant( 1152): wpa_supplicant v2 ...
- cf18B Platforms(仔细谨慎题)
题意: In one one-dimensional world there are n platforms. Platform with index k (platforms are numbere ...
- swagger3.0(springboot)消除basic-error-controller
1.新建springboot项目,可以通过https://start.spring.io/快速生成springboot项目. 2.引入jar依赖: <dependency> <gro ...