Leetcode 203 Remove Linked List Elements 链表

2023-04-20,,

去掉链表中相应的元素值

 /**
* 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 链表的相关教程结束。

《Leetcode 203 Remove Linked List Elements 链表.doc》

下载本文的Word格式文档,以方便收藏与打印。