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

203. Remove Linked List Elements

时间:2018-10-31 01:12:18      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:int   tno   问题   nod   lis   linked   ini   next   integer   

Remove all elements from a linked list of integers that have value val.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 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     if(!head)   return NULL;
10     struct ListNode *delete=head;
11     struct ListNode *n=head;
12     while(n){
13         if(head->val==val){
14             head=head->next;
15         }
16         else if(n->val==val){
17             delete->next=n->next;
18             n=n->next;
19             continue;
20         }
21         delete=n;
22         n=n->next;
23     }
24     return head;
25 }

 

203. Remove Linked List Elements

标签:int   tno   问题   nod   lis   linked   ini   next   integer   

原文地址:https://www.cnblogs.com/real1587/p/9880444.html

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