-
[LeetCode] 203. Remove Linked List ElementsLeetCode 2021. 11. 12. 11:41728x90
https://leetcode.com/problems/remove-linked-list-elements/
Remove Linked List Elements - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Remove Linked List Elements - Easy

LinkedList에서 주어진 val에 해당하는 노드를 삭제하고 양 옆 노드를 다시 이어줘야 한다.
노드를 한칸씩 훑으며, next노드의 val이 해당 val 일 경우 node->next를 node->next->next로 바꿔주는 작업을 반복해주자.
단 head부터 val일 경우엔 head = head->next로 head를 옮겨주자.
전체 풀이 코드
class Solution { public: ListNode* removeElements(ListNode* head, int val) { while (head && head->val==val) head = head->next; ListNode* node = head; while(node) { while(node->next && node->next->val == val) node->next=node->next->next; node = node->next; } return head; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 498. Diagonal Traverse (0) 2021.11.17 [LeetCode] 62. Unique Paths (0) 2021.11.17 [LeetCode] 1413. Minimum Value to Get Positive Step by Step Sum (0) 2021.11.11 [LeetCode] 781. Rabbits in Forest (0) 2021.11.10 [LeetCode] 1773. Count Items Matching a Rule (0) 2021.11.09