Remove Linked List Elements

删除链表元素

 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         ListNode * pre;
13         ListNode * temp;
14         temp=head;
15         pre=NULL;
16         while(temp!=NULL)
17         {
18             if(temp->val==val)
19             {
20                 if(pre!=NULL)
21                 {
22                     pre->next=temp->next;
23                 }
24                 else
25                     head=temp->next;
26             }
27             else
28             {
29                 pre=temp;
30             }
31             temp=temp->next;
32         }
33         return head;
34     }
35 };
原文地址:https://www.cnblogs.com/aguai1992/p/4629553.html