Leetcode 19. Remove Nth Node From End of List
2016-05-09 12:46
543 查看
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Note:
Given n will always be valid.
Try to do this in one pass.
题目大意:删除链表中倒数第n个节点。
题目分析:使用双指针相聚n-1,当第二个指针遍历到链表尾部则第一个指针就是要删除的节点指针。这里有个问题,因为要删除节点,所以不添加头结点的话,要把删除第一个节点,和其他节点区分开来(第一个节点没有前驱)。如果添加则可以统一处理。下面分别是两种代码,运行时间都是4ms
方法一(不添加头节点):
/**
* 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==NULL||head->next==NULL) return NULL;
ListNode* first=head;
ListNode* second=head;
for(int i=0;i<n-1;i++)
second=second->next;
if(second->next==NULL){//删除第一个节点
head=head->next;
return head;
}
while(second->next->next){
second=second->next;
first=first->next;
}
first->next=first->next->next;
return head;
}
};方法二(添加头节点):
/**
* 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==NULL) return NULL;
ListNode h(INT_MAX);
h.next=head;
ListNode *pfirst=&h;
ListNode *psecond=&h;
ListNode *temp;//释放被删除的空间
for(int i=0;i<n;i++)
psecond=psecond->next;
while(psecond->next){
psecond=psecond->next;
pfirst=pfirst->next;
}
temp=pfirst->next;
pfirst->next=pfirst->next->next;
delete temp;
return h.next;
}
};
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-1,当第二个指针遍历到链表尾部则第一个指针就是要删除的节点指针。这里有个问题,因为要删除节点,所以不添加头结点的话,要把删除第一个节点,和其他节点区分开来(第一个节点没有前驱)。如果添加则可以统一处理。下面分别是两种代码,运行时间都是4ms
方法一(不添加头节点):
/**
* 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==NULL||head->next==NULL) return NULL;
ListNode* first=head;
ListNode* second=head;
for(int i=0;i<n-1;i++)
second=second->next;
if(second->next==NULL){//删除第一个节点
head=head->next;
return head;
}
while(second->next->next){
second=second->next;
first=first->next;
}
first->next=first->next->next;
return head;
}
};方法二(添加头节点):
/**
* 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==NULL) return NULL;
ListNode h(INT_MAX);
h.next=head;
ListNode *pfirst=&h;
ListNode *psecond=&h;
ListNode *temp;//释放被删除的空间
for(int i=0;i<n;i++)
psecond=psecond->next;
while(psecond->next){
psecond=psecond->next;
pfirst=pfirst->next;
}
temp=pfirst->next;
pfirst->next=pfirst->next->next;
delete temp;
return h.next;
}
};
相关文章推荐
- C#中Ilist与list的区别小结
- C#中IList<T>与List<T>的区别深入解析
- C#对list列表进行随机排序的方法
- Vc++ 控件List Control用法总结
- C++实现的泛型List类分享
- C#使用IComparer自定义List类实现排序的方法
- C# Datagridview绑定List方法代码
- javascript radio list的实现细节(多浏览器兼容)
- c++ STL容器总结之:vertor与list的应用
- java实现List中对象排序的方法
- 大家注意vector, list, set, map成员函数erase
- ASP.NET―001:GridView绑定List、页面返回值具体实现
- List all the Databases on a SQL Server
- js Map List 遍历使用示例
- Java比较两个List的值是否相等的方法
- Java List双击事件实现方法
- velocity显示List与Map的方法详细解析
- 关于STL中list容器的一些总结
- 关于C#泛型列表List<T>的基本用法总结
- Java集合类中文介绍