203. 移除链表元素

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 struct ListNode* removeElements(struct ListNode* head, int val){
11 
12     struct ListNode *p = (struct ListNode *)malloc(sizeof(struct ListNode)),*q=head,*tail;
13     p->next = NULL;
14     tail = p;
15     while(q)
16     {
17         if(q->val==val)
18         {
19 
20         }else{
21             struct ListNode *s = (struct ListNode *)malloc(sizeof(struct ListNode));
22             s->val = q->val;
23             s->next = NULL;
24             tail->next = s;
25             tail = s;
26         }
27 
28         q = q->next;
29     }
30 
31     return p->next;
32 }

 

posted @ 2021-06-16 15:45  传说中的旅行者  阅读(21)  评论(0)    收藏  举报