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

203. Remove Linked List Elements

时间:2016-05-19 23:14:52      阅读:166      评论: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  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* removeElements(struct ListNode* head, int val) {
 9     struct ListNode* cur;
10     struct ListNode* pre;
11     if(NULL == head)
12         return head;
13     cur = head;
14     pre = head;
15     while(cur != NULL)
16     {
17         if(val == head->val)               //注意删除头结点的情况!
18         {
19             head = head->next;
20             cur = head;
21             pre = head;
22             continue;
23         }
24         if(val == cur->val)
25         {
26             pre->next = cur->next;
27             cur = cur->next;
28             continue;
29         }
30         pre = cur;
31         cur = cur->next;
32     }
33     return head;
34 }

 

203. Remove Linked List Elements

标签:

原文地址:http://www.cnblogs.com/boluo007/p/5510505.html

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