Leetcode 203 Remove Linked List Elements 链表

去掉链表中相应的元素值

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(!head) return NULL;
ListNode* now = head;
for(;now->next ;){
if(now->next->val == val){
ListNode* next = now->next;
now->next = next->next;
delete next;
}
else now = now->next;
}
if(head->val == val) {
ListNode* t = head;
head = head->next;
delete t;
}
return head;
}
};
上一篇:[LeetCode] 203. Remove Linked List Elements 解题思路


下一篇:使用scp在windows和Linux之间互传文件