标签:
题目描述:
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
代码:
class Solution { public: ListNode * removeElements(ListNode * head,int val) { ListNode * new_node = new ListNode(-1); new_node->next = head; ListNode * first = new_node; ListNode * second = new_node->next; while(second != NULL) { if(second->val == val) { first->next = second->next; second = first->next; } else { first = second; second = second->next; } } ListNode * temp = new_node->next; delete new_node; return temp; } }
LeetCode:Remove Linked List Elements
标签:
原文地址:http://blog.csdn.net/yao_wust/article/details/45645219