码迷,mamicode.com
首页 > 其他好文 > 详细

203. Remove Linked List Elements 删除链表中val

时间:2017-10-24 20:53:45      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:color   amp   null   else   bsp   tno   hat   turn   rem   

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

标签:color   amp   null   else   bsp   tno   hat   turn   rem   

原文地址:http://www.cnblogs.com/xumh/p/7725352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!