首页 > 其他 > 详细

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

时间:2017-10-24 20:17:50      阅读:262      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!