203. Remove Linked List Elements 删除链表中val
Posted lvbbg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了203. Remove Linked List Elements 删除链表中val相关的知识,希望对你有一定的参考价值。
203. Remove Linked List Elements
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
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* removeElements(ListNode* head, int val) { 12 ListNode *pre = new ListNode(-1), *cur = pre; 13 pre->next = head; 14 while (cur->next != nullptr) { 15 if (cur->next->val == val) { 16 ListNode *t = cur->next; 17 cur->next = cur->next->next; 18 t->next = nullptr; 19 delete t; 20 } else { 21 cur = cur->next; 22 } 23 } 24 return pre->next; 25 } 26 };
以上是关于203. Remove Linked List Elements 删除链表中val的主要内容,如果未能解决你的问题,请参考以下文章
203. Remove Linked List Elementseasy
#Leetcode# 203. Remove Linked List Elements
203. Remove Linked List Elements
203. Remove Linked List Elements