[LintCode]Remove Linked List Elements(C++)

xiaoxiao2021-02-28  126

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: /** * @param head a ListNode * @param val an integer * @return a ListNode */ ListNode *removeElements(ListNode *head, int val) { // Write your code here ListNode **p = &head; while (*p) { if ((*p)->val == val) { if ((*p)->next == NULL){ delete *p; *p = NULL; }else{ ListNode *x = *p; *p = (*p)->next; delete x; } }else p = &(*p)->next; } return head; } };
转载请注明原文地址: https://www.6miu.com/read-25201.html

最新回复(0)