leetcode个人题解——#19 Remove Nth Node From End of List
思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作。
当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分配的内存空间,要加一个条件判断。
/**
* 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) {
if(head->next == NULL && n == ) return NULL;
if(n == ) return head;
ListNode* p = head;
ListNode* q = head;
for (int i = ; i < n; i++)
p = p->next;
int sum = ;
while(p != NULL && p->next != NULL)
{
p = p->next;
q = q->next;
sum++;
}
if(sum == && p == NULL) return q->next;
q->next = q->next->next;
return head;
delete p;
delete q;
delete head;
}
};
leetcode个人题解——#19 Remove Nth Node From End of List的更多相关文章
- 《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
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the ...
- 【leetcode❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- 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. ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【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题解:(19) Remove Nth Node From End of List
题目说明 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
随机推荐
- 使用 Solr 构建企业级搜索服务器
最近因项目需要一个全文搜索引擎服务, 在考察了Lucene及Solr后,我们选择了Solr. 本文简要记录了基于Solr搭建一个企业搜索服务器的过程.网上的资料太多千篇一律,也可能版本不同,总之在参照 ...
- 产品 | What's产品经理
如果想知道什么是产品,首先需要知道什么是缔造者.其名曰:"产品经理". PS:产品经理一词在国内大多时候泛指"互联网产品经理". 对于产品经理这一职位,说实在很 ...
- Codeforces Round #487 (Div. 2)
A. A Blend of Springtime(暴力/模拟) 题目大意 给出$n$个花,每个点都有自己的颜色,问是否存在连续大于等于三个花颜色均不相同 sol 直接模拟判断即可 #include&l ...
- Linux系统结构 详解(转)
Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统.部分层次结构如图1-1所 ...
- Thinkphp5 使用composer中seeder播种机
前因: 前几天,客户要求做一个会员问答的系统,我就按流程做了,到了需要调用数据库数据时,觉得一个个添加又有点笨~ 解决过程: 后来查了查手册,看看国外blog案例,我搞出来了个不错的方法~~~ 我的使 ...
- 【mongodb分片中mogos启动的报错】
- 使用Letsencrypt做SSL certificate
为什么要使用Letsencrypt做SSL certificate? 最简单直接的原因是免费.但是免费存在是否靠谱的问题,尤其是对安全要求比较高的网站,需要考虑使用letsencrypt的安全性是否符 ...
- Python习题(分页显示)
class Page: def __init__(self, lst, pageSize): self.lst = lst # 数据 self.pageSize = pageSize # 每页显示多少 ...
- Div标签使用inline-block有间距
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 20145202马超 2016-2017-2 《Java程序设计》第二次实验
去年完成的一部分(http://www.cnblogs.com/tuolemi/p/5728826.html) 今年我又从新做的,这是分别5个问题做出来的结果 下面这个是去年没有做的,是用来建模的,感 ...