您的位置:首页 > 其它

leetcode 203: Remove Linked List Elements

2015-08-26 12:26 459 查看
Create a dummy head before head. The rest is easy.

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyhead=new ListNode(-1);
dummyhead->next=head;
ListNode *curr=dummyhead;
while(curr->next)
{
if(curr->next->val==val)
{
ListNode *temp=curr->next;
curr->next=temp->next;
delete temp;
}
else
curr=curr->next;
}
head=dummyhead->next;
delete dummyhead;
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: