Created
December 25, 2018 23:19
-
-
Save kanrourou/7bec00871fb669965d892f3cb420a681 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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* helper = new ListNode(-1); | |
| helper->next = head; | |
| auto curr = helper; | |
| while(curr->next) | |
| { | |
| while(curr->next && curr->next->val != val) | |
| curr = curr->next; | |
| if(!curr->next)break; | |
| auto tmp = curr->next; | |
| curr->next = tmp->next; | |
| tmp->next = nullptr; | |
| } | |
| return helper->next; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment