Remove Nth Node From End of List
2015-06-09 16:48
651 查看
题目:
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.
思路1:
先将链表逆转,然后删除相应位的元素,再次逆转,输出链表
思路2:
设定两个指针,二者相差n-1,当后面的一个指针为空时,前一个指针即为所要删除的节点
代码:
#include <iostream>
using namespace std;
/**
* 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) { //思路1
/*将链表逆转*/
if(head==NULL) return head;//head为空直接返回
if(head->next==NULL && n==1) //链表只包含一个元素,直接删除
{
head=NULL;
return head;
}
ListNode *q; //用于记录头结点
ListNode *nex;//下一个节点
ListNode *pre=NULL;//前一个节点
ListNode *cur=head;//当前节点
while(cur!=NULL)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
head=pre;
/*删除特定位置上的值*/
q=head;
if(n==1)
{
head=head->next;
}
else
{
while(--n)
{
pre=q;
q=q->next;
}
pre->next=q->next;
}
/*再次将链表逆转*/
pre=NULL;
cur=head;
while(cur!=NULL)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
head=pre;
return head;
}
ListNode* removeNthFromEnd2(ListNode* head, int n) //思路2
{
if(head==NULL) return head;
ListNode *first,*last,*pre=NULL;
first=last=head;
n--;
while(n--&&last!=NULL)
{
last=last->next;
}
while(last->next!=NULL)
{
pre=first;
first=first->next;
last=last->next;
}
if(pre ==NULL)
{
head=first->next;
delete first;
}
else
{
pre->next=first->next;
delete first;
}
return head;
}
};
int main()
{
ListNode *head=new ListNode(1);
ListNode *p;
p=head;
/*
p->next=new ListNode(2);
p=p->next;
p->next=new ListNode(3);
p=p->next;
p->next=new ListNode(4);
p=p->next;
p->next=new ListNode(5);
p=p->next;
*/
Solution s;
head=NULL;
head=s.removeNthFromEnd2(head,1);
while(head!=NULL)
{
cout<<head->val<<"->";
head=head->next;
}
system("pause");
return 0;
}
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.
思路1:
先将链表逆转,然后删除相应位的元素,再次逆转,输出链表
思路2:
设定两个指针,二者相差n-1,当后面的一个指针为空时,前一个指针即为所要删除的节点
代码:
#include <iostream>
using namespace std;
/**
* 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) { //思路1
/*将链表逆转*/
if(head==NULL) return head;//head为空直接返回
if(head->next==NULL && n==1) //链表只包含一个元素,直接删除
{
head=NULL;
return head;
}
ListNode *q; //用于记录头结点
ListNode *nex;//下一个节点
ListNode *pre=NULL;//前一个节点
ListNode *cur=head;//当前节点
while(cur!=NULL)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
head=pre;
/*删除特定位置上的值*/
q=head;
if(n==1)
{
head=head->next;
}
else
{
while(--n)
{
pre=q;
q=q->next;
}
pre->next=q->next;
}
/*再次将链表逆转*/
pre=NULL;
cur=head;
while(cur!=NULL)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
head=pre;
return head;
}
ListNode* removeNthFromEnd2(ListNode* head, int n) //思路2
{
if(head==NULL) return head;
ListNode *first,*last,*pre=NULL;
first=last=head;
n--;
while(n--&&last!=NULL)
{
last=last->next;
}
while(last->next!=NULL)
{
pre=first;
first=first->next;
last=last->next;
}
if(pre ==NULL)
{
head=first->next;
delete first;
}
else
{
pre->next=first->next;
delete first;
}
return head;
}
};
int main()
{
ListNode *head=new ListNode(1);
ListNode *p;
p=head;
/*
p->next=new ListNode(2);
p=p->next;
p->next=new ListNode(3);
p=p->next;
p->next=new ListNode(4);
p=p->next;
p->next=new ListNode(5);
p=p->next;
*/
Solution s;
head=NULL;
head=s.removeNthFromEnd2(head,1);
while(head!=NULL)
{
cout<<head->val<<"->";
head=head->next;
}
system("pause");
return 0;
}
相关文章推荐
- 【LeetCode】24.Swap Nodes in Pairs
- Node.js之缓冲模块Buffer
- Node Callbacks
- Node.js原理介绍
- [Selenium]Grid模式下运行时打印出当前Case在哪台node机器上运行
- css字体转换程序(Node.js)
- 基于nodejs的开发环境搭建
- 对于node.js开发指南中博客案例的修改实现(一)--开发环境搭建
- OSG程序设计之osg::NodeVisitor
- 使用yo angular-fullstack 开发基于node angular mongo程序的环境搭建
- Count Complete Tree Nodes
- Count Complete Tree Nodes
- ENode通信层性能测试结果
- Count Complete Tree Nodes - LeetCode 222
- openstack neutron 分析之network node报文跟踪
- [leetcode] Count Complete Tree Nodes
- Count Complete Tree Nodes
- Sublime2 运行NodeJS
- LeetCode 25: Reverse Nodes in k-Group
- [LeetCode] Count Complete Tree Nodes