203. Remove Linked List Elements
https://leetcode.com/problems/remove-linked-list-elements/
Language: C++
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head != NULL) {
ListNode* prev = NULL;
ListNode* cur = head;
while (cur) {
if (cur->val == val) {
if (cur == head) {
head = head->next;
} else {
prev->next = cur->next;
}
} else {
prev = cur;
}
cur = cur->next;
}
}
return head;
}
};
728x90
반응형
'자료구조 알고리즘 > 코딩테스트' 카테고리의 다른 글
Merge Two Sorted Lists (0) | 2022.01.28 |
---|---|
Odd Even Linked List (0) | 2021.07.07 |
Reverse Linked List (0) | 2021.07.04 |
Remove Nth Node From End of List (0) | 2021.07.03 |
Intersection of Two Linked Lists (0) | 2021.07.03 |