首页 > 其他 > 详细

Remove Linked List Elements

时间:2015-12-25 14:58:43      阅读:154      评论:0      收藏:0      [点我收藏+]

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         
13         ListNode node(-1);
14         node.next=head;
15         ListNode *pre=&node;
16         while(head!=NULL)
17         {
18             if(head->val==val)
19             {
20                 pre->next=head->next;
21                 delete head;
22                 head=pre->next;
23             }
24             else
25             {
26                 pre=pre->next;
27                 head=head->next;
28             }
29 
30         }
31         return node.next;
32     }
33 };

 

Remove Linked List Elements

原文:http://www.cnblogs.com/chess/p/5075663.html

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