您的位置:首页 > Web前端 > Node.js

leetcode 19: Remove Nth Node From End of List

2015-07-03 11:57 666 查看
/**
* 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) {
int len=0;
ListNode *p=head;
if(!p)
return head;
while(p)
{
len++;
p=p->next;
}
n=len-n;
if(n==0)
head=head->next;
else
{
p=head;
for(int i=1;i!=n;i++)
p=p->next;
p->next=p->next->next;
}
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: