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,
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.
简单的指针操作。
/**
* 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 *pn=head;
int sum=;
while(pn->next){
pn=pn->next;
sum++;
}
int tn=sum-n+;
if(tn==){
return head->next;
}
pn=head;
int i=;
while(i!=tn-){
pn=pn->next;
i++;
}
pn->next = pn->next->next;
return head; }
};
leetcode 19. Remove Nth Node From End of List(链表)的更多相关文章
- Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...
- [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 ...
随机推荐
- Xshell 一款很养眼的配色方案推荐
Xshell 是个很好用的在 windows 下登陆 liunx 的终端原生支持中文,配合 Xftp 管理文件,同是免费软件可远比 Putty 好用多了面对枯燥的代码,我们需要一款很养眼的配色方案来保 ...
- HTTP头解读
Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源, 而HTTP中的GET.POST.PUT. DELETE ...
- eeplat开发平台概念理解
近期在学习eeplat的开发.发现其中有非常多概念实在让人easy忘记,所以谨以此文记录一笔. eeplat的开发文档里说eeplat是元数据驱动的,这个元数据什么意思.在我理解就是后台的数据库里面的 ...
- StringBuilder作用
String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然 ...
- matlab2017b linux版分享
链接:https://pan.baidu.com/s/1smrTkFN 密码:cvb3 下载后请点关注并点赞,谢谢支持.
- Mongodb之备份恢复脚本
本分脚本: !/bin/bash #备份文件执行路径 which mongodump DUMP= #临时备份目录 OUT_DIR= #本分存放目录 TAR_DIR= #获取当前系统时间==> 2 ...
- 查看mysql 的存储过程定义
查询数据库中的存储过程 方法一: select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE' 方法 ...
- python 基础 5.1 python 构造器
一. 类的构造器 __init__ 构造函数,在生成对象时调用.由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去.通过定义一个特殊的__init__方法, ...
- npm ERR! fatal: unable to connect to github.com
https://blog.csdn.net/baidu_30809315/article/details/86520093 git config --global url."https:// ...
- 九度OJ 1065:输出梯形 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5379 解决:2939 题目描述: 输入一个高度h,输出一个高为h,上底边为h的梯形. 输入: 一个整数h(1<=h<=1000 ...