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

Remove Nth Node From End of List

2015-06-04 14:57 399 查看
/**
* 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* newHead= new ListNode(-1);
newHead->next=head;
ListNode* first=newHead;
ListNode* second=head;
while(--n)
second=second->next;
while(second->next!=NULL)
{
first=first->next;
second=second->next;
}
first->next=first->next->next;

return newHead->next;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: