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

203. Remove Linked List Elements

时间:2018-01-09 11:13:19      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:null   pre   str   blog   span   相同   节点   rom   move   

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

 

删除链表中跟val相同的节点

 

C++(32ms):

 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* preHead = new ListNode(0) ;
13         preHead->next = head ;
14         ListNode* cur = preHead ;
15         while(cur){
16             if (cur->next && cur->next->val == val)
17                 cur->next = cur->next->next ;
18             else
19                 cur = cur->next ;
20         }
21         return preHead->next ;
22     }
23 };

 

203. Remove Linked List Elements

标签:null   pre   str   blog   span   相同   节点   rom   move   

原文地址:https://www.cnblogs.com/mengchunchen/p/8250261.html

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