452. Remove Linked List Elements【Naive】

Remove all elements from a linked list of integers that have value val.

Example

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

解法一:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param head a ListNode
* @param val an integer
* @return a ListNode
*/
ListNode *removeElements(ListNode *head, int val) {
ListNode * dummy = new ListNode(-);
dummy->next = head;
head = dummy; while (head->next != NULL) {
if (head->next->val == val) {
head->next = head->next->next;
continue;
} head = head->next;
} return dummy->next;
}
};
上一篇:Oracle 10g DG 环境搭建详解


下一篇:如何在android studio 1.0 启动时设置代理【解决WARN - ateSettings.impl.UpdateChecker - Connection failed.】