LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)
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.
Note:
Given n will always be valid.
Try to do this in one pass.
只能走一遍,要求移除倒着数的第n个元素,开始想用递归做,但是一直失败,没办法看了一下别人的答案,如果总数为N那么倒着第n个相当于正着第N-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 * p, * q, * pPre;
pPre = NULL;
p = q = head;
while(--n > )
q = q->next;
while(q->next){
pPre = p;
p = p->next;
q = q->next; }
if(pPre == NULL){
head = p->next;
delete p;
}else{
pPre->next = p->next;
delete p;
}
return head;
}
};
LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)的更多相关文章
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- leetcode 【 Remove Nth Node From End of List 】 python 实现
题目: 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 ...
- 【leetcode】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】Remove Nth Node From End of List(easy)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- 【JAVA、C++】LeetCode 019 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, Give ...
- Java [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
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
随机推荐
- 2016 安全行业全景图——By 安全牛
2014年有幸在北京办公室与安全牛的创办人刘朝阳见过一面,从那以后一直关注这安全牛(http://www.aqniu.com/)以及IT经理网(http://www.ctocio.com/).今年初看 ...
- Nordic Blue Tooth
一 . nordic BLE4.0 1.开发nordic的应用需要安装支持keil的pack库和插件 2.nordic的SDK很完整,实例涵盖了几乎所有的应用 https://www.nordicse ...
- 用python的turtle画分形树
由于分形树具有对称性,自相似性,所以我们可以用递归来完成绘制.只要确定开始树枝长.每层树枝的减短长度和树枝分叉的角度,我们就可以把分形树画出来啦!! 代码如下: # -*- coding: utf-8 ...
- 吴超老师课程---Hadoop的伪分布安装
1.1 设置ip地址 执行命令 service network restart 验证: ifconfig1.2 关闭防火墙 执行命令 service ip ...
- C# 建立UDP服务器并接收客户端数据
C# 建立UDP服务器并接收客户端数据 2015-02-11 17:20 1218人阅读 评论(0) 收藏 举报 分类: C#开发技术(22) 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- centos7 上搭建私有云
OwnCloud环境搭建 一. 环境搭建 1. 环境需求 服务器操作系统:Centos7.0 外网服务器操作系统:Centos7.0 Php版本号:5.4.16 Mysql版本号:5.5.52 Apa ...
- js踩过的一些坑
参考我的博客:http://www.isedwardtang.com/2017/08/29/js-bug/
- spring boot Rabbitmq集成,延时消息队列实现
本篇主要记录Spring boot 集成Rabbitmq,分为两部分, 第一部分为创建普通消息队列, 第二部分为延时消息队列实现: spring boot提供对mq消息队列支持amqp相关包,引入即可 ...
- 安装pycurl
环境:ubuntu 1604 安装 pycurl 遇到一些问题 简单记录 1.安装 pippython2:apt install python-pippython3: apt install pyth ...
- discuz对PHP7不支持mysql的兼容性处理
PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...