2019-01-31 Day26
2019-01-31 本文已影响0人
骚得过火
1.移除链表元素
删除链表中等于给定值 val 的所有节点。
示例:
输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
/**
* 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 * listhead = new ListNode(0);
listhead ->next = head;
ListNode * cur = head ,*headlist = listhead;
while( cur )
{
if(cur -> val == val)
listhead ->next = cur -> next;
else
listhead = listhead->next;
cur = cur ->next;
}
return headlist->next;
}
};