【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列
(一)题目
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.
(二)解题
剑指上面的经典题目了。两个指针,一个指向head,一个指向正数第K-1个,同时移动,知道第K-1的指针指向NULL。则指向head的数就是倒数第K个。
删除节点的时候要区分头节点和其他节点。
/**
* 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(n == 0) return head;
ListNode* ptemp = head;
for(int i = 0 ; i < n ;i++)
{
ptemp = ptemp->next;
}
ListNode* pNth = head;
ListNode* pre = head;
while(ptemp!= NULL)
{
pre = pNth;
ptemp = ptemp->next;
pNth = pNth->next;
}
if(pNth == head)//如果是头节点
{
head = head->next;
}
else pre->next = pNth->next;//非头节点
free(pNth);
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 ...
- 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 ...
随机推荐
- iOS开源加密相册Agony的实现(五)
简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...
- Android 内置群组,联系人
这样一个需求,手机第一次启动的时候,需要内置一个群组,并且里面有给定的联系人信息, 本来打算写双进程守护的,结果昨天接到一个这样的任务,就先把它做了,发现里面有些操作数据库的东西还是值得看一下. 首先 ...
- J2EE进阶(十五)MyEclipse反向工程实现从数据库反向生成实体类之Hibernate方式
J2EE进阶(十五)MyEclipse反向工程实现从数据库反向生成实体类之Hibernate方式 反向工程又称逆向工程. 开发项目涉及到的表太多,一个一个的写JAVA实体类很是费事.MyEcl ...
- #pragma pack(x) CPU对齐
编译器会尽量把成员对齐以提高内存的命中率.对齐是可以更改的,使用"#pragma pack(x)" 可以改变编译器的对齐方式. C++固有类型的对界取编译器对齐方式与自身大小中较小 ...
- zk日常运维管理
清理数据目录 dataDir目录指定了ZK的数据目录,用于存储ZK的快照文件(snapshot).另外,默认情况下,ZK的事务日志也会存储在这个目录中.在完成若干次事务日志之后(在ZK中,凡是对数据有 ...
- 浅谈hibernate+入门实例
Hibernate是对jdbc进一步的封装,随着项目的开展,小编开始接触到这个概念,一开始接触的时候并没有觉得hibernate有多神秘,没有进一步的研究,只是简单的知道她是对jdbc的进一步的封装, ...
- 剑指Offer——银行网申内容模版
剑指Offer--银行网申内容模版 年获得"优秀共青团员"称号 2013.12 科技标兵 2013.10 三好学生 2013.10 "三下乡"优秀学生 2013 ...
- Android实现横屏以及全屏的小技巧
分享两个安卓的实用小技巧,那就是横屏和全屏的实现. 首先是横屏的实现 首先是在清单文件中实现 <activity android:name=".MainActivity" a ...
- 安卓AsyncTack详解
我们知道安卓中的UI线程不是线程安全的,即不能在UI线程中进行耗时操作,所以我们通常的做法是开启一个子线程来进行耗时操作,然后将处理后的结果运用Handler机制传递给UI线程,在UI线程中根据处理后 ...
- Nginx中502和504错误详解
在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决方案. 1.502 Bad ...