Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点
我的做法是将两个指针first,second
先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next->next为空
最后只要删除second->next
/**
* 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) return head; ListNode* first = head;
for(int i = ; i < n - ; first = first->next, ++i); if(!first->next){
first = head;
head = head->next;
delete first;
return head;
} ListNode* second= head;
for(;first->next->next; first = first->next, second = second->next); ListNode* next = second->next;
second->next = next->next;
delete next; return head;
}
};
Leetcode 19 Remove Nth Node From End of List 链表的更多相关文章
- [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 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 [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 ...
- (链表 双指针) leetcode 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [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 ...
- [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
随机推荐
- HDOJ 4508 湫湫系列故事——减肥记I (完全背包带优化)
完全背包的模版题.. 加了一个小优化 n^2的写法 O(V+N)在本题中复杂度较高 不采纳 完全背包问题有一个很简单有效的优化,是这样的:若两件物品i.j满足c[i]<=c[j]且w[i]&g ...
- 第一章 tomcat安装与启动
一.安装 1.下载tomcat安装包 2.解压安装包 3.配置环境变量 打开~/.bash_profile文件,输入一下两句话: export TOMCAT_HOME=/Users/enniu1/De ...
- JDBC第一天
(一)JDBC访问数据库的工作过程: <1>加载驱动,建立连接 oracle数据库:在项目中导入ojdbc.jar包 数据库驱动路径:String driver ...
- ADB理解
在做手机测试时候,经常用到的命令就是adb.如adb shell,adb devices,adb logcat等等 那么什么是adb,怎么用呢? 一.adb adb的全称为Android Debug ...
- three.js 源码注释(四十四)Light/DirectionalLight.js
/** * * DirectionalLight方法 根据设置灯光的颜属性color, 强度属性intensity创建平行光光源. * DirectionalLight 对象的功能函数采用定义构造的函 ...
- Daily Scrum 12.2
今日完成任务: 完成数据库文档说明:整理数据库(没整理完),明天继续:文档功能的修改以及在服务器上测试 明日任务: 黎柱金 整理数据库 孙思权 整理数据库 晏旭瑞 文档上传下载功能 冯飘飘 修复问题提 ...
- P1-概率论基础(Primer on Probability Theory)
2.1概率密度函数 2.1.1定义 设p(x)为随机变量x在区间[a,b]的概率密度函数,p(x)是一个非负函数,且满足 注意概率与概率密度函数的区别. 概率是在概率密度函数下对应区域的面积,如上图右 ...
- Azure china服务状态报告查看网址
https://www.azure.cn/support/service-dashboard/
- 有关pascal的填充语句小技巧
背景 今天打代码,用了一次fillchar(a,sizeof(a),1); 结果a数组(of longint)所赋的值却不是1 探索 ···pascal program fillchartest; v ...
- 6.1-6.5关于html
网页一般是两种元素组合起来的,一种是内联元素, 也就是行内显示,加上width和height没效果.一种是区块元素,可以加上对应的width和height, 通常使用在网页的布局,最常用的就是< ...