Remove Linked List Elements Solutions in C++
Number 203
Difficulty Easy
Acceptance 38.7%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/remove-linked-list-elements/// Author : Hao Chen// Date : 2015-06-09/*** 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) {static ListNode dummy(-1);dummy.next = head;ListNode *p = &dummy;while( p->next) {if (p->next->val == val) {p->next = p->next->next;}else{p = p->next;}}return dummy.next;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description//// Author : liuyubobobo/// Time : 2017-11-15#include <iostream>using namespace std;///Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// LinkedList Test Helper FunctionsListNode* createLinkedList(int arr[], int n){if(n == 0)return NULL;ListNode* head = new ListNode(arr[0]);ListNode* curNode = head;for(int i = 1 ; i < n ; i ++){curNode->next = new ListNode(arr[i]);curNode = curNode->next;}return head;}void printLinkedList(ListNode* head){if(head == NULL){cout << "NULL" << endl;return;}ListNode* curNode = head;while(curNode != NULL){cout << curNode->val;if(curNode->next != NULL)cout << " -> ";curNode = curNode->next;}cout << endl;return;}void deleteLinkedList(ListNode* head){ListNode* curNode = head;while(curNode != NULL){ListNode* delNode = curNode;curNode = curNode->next;delete delNode;}return;}/// Linear Scan with dummy head/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:ListNode* removeElements(ListNode* head, int val) {ListNode* dummyHead = new ListNode(0);dummyHead->next = head;ListNode* cur = dummyHead;while(cur->next != NULL){if(cur->next->val == val){ListNode* delNode = cur->next;cur->next = delNode->next;delete delNode;}elsecur = cur->next;}ListNode* retNode = dummyHead->next;delete dummyHead;return retNode;}};int main() {int arr[] = {1, 2, 6, 3, 4, 5, 6};int n = sizeof(arr) / sizeof(int);ListNode* head = createLinkedList(arr, n);printLinkedList(head);Solution().removeElements(head, 6);printLinkedList(head);deleteLinkedList(head);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description//// Author : liuyubobobo/// Time : 2018-09-17#include <iostream>using namespace std;///Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Recursive/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:ListNode* removeElements(ListNode* head, int val) {if(!head)return head;if(head->val == val){ListNode* node = head->next;delete head;return removeElements(node, val);}head->next = removeElements(head->next, val);return head;}};int main() {return 0;}