首页 > 其他 > 详细

203. Remove Linked List Elements

时间:2018-10-31 00:42:22      阅读:151      评论:0      收藏:0      [点我收藏+]

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

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

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