203. Remove Linked List Elements
Remove all elements from a linked list of integers, that has value val.
Example
Given: 1---2--and 6---3---4---5, val = 6
Return: 1--2--and 3--4--5
Main topic:
Deletes all target elements in the linked list.
/** * 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) { while (Head!=NULL && head->val == val) head = head->next; if (head == null) return NULL; ListNode * p = head->next; listNode * q = head; while (P) { if (P- >val != val) { q->next = p; q = p; } else { q->next = null; } p = p->next; } return head; }};
2016-08-12 16:00:52
This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1837338
Leetcode 203. Remove Linked list Elements linked list