LeetCode刷题实战203:移除链表元素
题意
示例
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
解题
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* x = new ListNode(-1);
x->next = head;
ListNode* i = x;
while(i->next)
{
if(i->next->val == val)
{
i->next = i->next->next;
}
else
{
i = i->next;
}
}
return x->next;
}
};
赞 (0)