标签:
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5
这道题很简单,要求删除单链表中与指定值相等的结点。在遍历单链表的基础上完成比较,增设一个头结点,方便操作。
ListNode* removeElements(ListNode* head, int val) {
ListNode* h = new ListNode(0);
h->next = head;
ListNode* p = h;
while (p->next){
if (p->next->val == val){
ListNode* temp = p->next;
p->next = temp->next;
delete temp;
}
else{
p = p->next;
}
}
return h->next;
}
[LeetCode]Remove Linked List Elements
标签:
原文地址:http://blog.csdn.net/kaitankedemao/article/details/45252203