leetcode 203 移除链表元素

地址:https://leetcode-cn.com/problems/remove-linked-list-elements/
大意:删除链表中等于给定值的所有节点。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == NULL)
            return head;
        ListNode *hh = new ListNode(0);
        hh->next = head;
        ListNode *newNode = hh;
        while(newNode != NULL && newNode->next != NULL){
            if(newNode->next->val == val){
                newNode->next = newNode->next->next;
            }else{
                newNode = newNode->next;
            }
        }
        return hh->next;
    }
};

猜你喜欢

转载自www.cnblogs.com/c21w/p/12683523.html