leetcode 日经贴,Cpp code -Remove Linked List Elements

来源:互联网 发布:unity3d中国官网 编辑:程序博客网 时间:2024/06/03 03:44

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) {        ListNode *h = NULL, *cur = NULL;        while (head) {            if (head->val != val) {                if (!h) {                    h = cur = head;                } else {                    cur->next = head;                    cur = head;                }            }            head = head->next;        }        if (cur) {            cur->next = NULL;        }        return h;    }};


0 0