Remove value from linked list if it is equal to a parameter











up vote
0
down vote

favorite













Problem: Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.




My solution works, but I suspect it's not smart and optimized because I have many extra variables to solve this question.



Node.h:



#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

template<typename T>
struct ListNode {
ListNode(const T &v) : value(v), next(nullptr) {}
T value;
ListNode *next;
};

#endif // NODE_H_INCLUDED


main.cpp:



#include<iostream>
#include "Node.h"

void print(ListNode<int> * l){
while(l != nullptr){
std::cout << l->value << "n";
l = l->next;
}
}

ListNode<int> * removeKFromList(ListNode<int> * l, int k) {
if(l == nullptr){
return nullptr;
}else{
if(l->value != k){
ListNode<int> * L2 = new ListNode<int>(l->value);
ListNode<int> * current = L2;
l = l->next;
while(l != nullptr){
if(l->value != k){
ListNode<int> * L3 = new ListNode<int>(l->value);
while(current->next != nullptr){
current = current->next;
}
current->next = L3;
}
l = l->next;
}
return L2;
}else{
return removeKFromList(l->next, k);
}
}
}

int main(){
ListNode<int> * L = new ListNode<int>(5);
ListNode<int> * L1 = new ListNode<int>(6);
ListNode<int> * L2 = new ListNode<int>(8);
L1->next = L2;
L->next = L1;
print(removeKFromList(L, 7));
return 0;
}









share|improve this question
























  • That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
    – 200_success
    7 hours ago















up vote
0
down vote

favorite













Problem: Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.




My solution works, but I suspect it's not smart and optimized because I have many extra variables to solve this question.



Node.h:



#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

template<typename T>
struct ListNode {
ListNode(const T &v) : value(v), next(nullptr) {}
T value;
ListNode *next;
};

#endif // NODE_H_INCLUDED


main.cpp:



#include<iostream>
#include "Node.h"

void print(ListNode<int> * l){
while(l != nullptr){
std::cout << l->value << "n";
l = l->next;
}
}

ListNode<int> * removeKFromList(ListNode<int> * l, int k) {
if(l == nullptr){
return nullptr;
}else{
if(l->value != k){
ListNode<int> * L2 = new ListNode<int>(l->value);
ListNode<int> * current = L2;
l = l->next;
while(l != nullptr){
if(l->value != k){
ListNode<int> * L3 = new ListNode<int>(l->value);
while(current->next != nullptr){
current = current->next;
}
current->next = L3;
}
l = l->next;
}
return L2;
}else{
return removeKFromList(l->next, k);
}
}
}

int main(){
ListNode<int> * L = new ListNode<int>(5);
ListNode<int> * L1 = new ListNode<int>(6);
ListNode<int> * L2 = new ListNode<int>(8);
L1->next = L2;
L->next = L1;
print(removeKFromList(L, 7));
return 0;
}









share|improve this question
























  • That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
    – 200_success
    7 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite












Problem: Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.




My solution works, but I suspect it's not smart and optimized because I have many extra variables to solve this question.



Node.h:



#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

template<typename T>
struct ListNode {
ListNode(const T &v) : value(v), next(nullptr) {}
T value;
ListNode *next;
};

#endif // NODE_H_INCLUDED


main.cpp:



#include<iostream>
#include "Node.h"

void print(ListNode<int> * l){
while(l != nullptr){
std::cout << l->value << "n";
l = l->next;
}
}

ListNode<int> * removeKFromList(ListNode<int> * l, int k) {
if(l == nullptr){
return nullptr;
}else{
if(l->value != k){
ListNode<int> * L2 = new ListNode<int>(l->value);
ListNode<int> * current = L2;
l = l->next;
while(l != nullptr){
if(l->value != k){
ListNode<int> * L3 = new ListNode<int>(l->value);
while(current->next != nullptr){
current = current->next;
}
current->next = L3;
}
l = l->next;
}
return L2;
}else{
return removeKFromList(l->next, k);
}
}
}

int main(){
ListNode<int> * L = new ListNode<int>(5);
ListNode<int> * L1 = new ListNode<int>(6);
ListNode<int> * L2 = new ListNode<int>(8);
L1->next = L2;
L->next = L1;
print(removeKFromList(L, 7));
return 0;
}









share|improve this question
















Problem: Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k.




My solution works, but I suspect it's not smart and optimized because I have many extra variables to solve this question.



Node.h:



#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED

template<typename T>
struct ListNode {
ListNode(const T &v) : value(v), next(nullptr) {}
T value;
ListNode *next;
};

#endif // NODE_H_INCLUDED


main.cpp:



#include<iostream>
#include "Node.h"

void print(ListNode<int> * l){
while(l != nullptr){
std::cout << l->value << "n";
l = l->next;
}
}

ListNode<int> * removeKFromList(ListNode<int> * l, int k) {
if(l == nullptr){
return nullptr;
}else{
if(l->value != k){
ListNode<int> * L2 = new ListNode<int>(l->value);
ListNode<int> * current = L2;
l = l->next;
while(l != nullptr){
if(l->value != k){
ListNode<int> * L3 = new ListNode<int>(l->value);
while(current->next != nullptr){
current = current->next;
}
current->next = L3;
}
l = l->next;
}
return L2;
}else{
return removeKFromList(l->next, k);
}
}
}

int main(){
ListNode<int> * L = new ListNode<int>(5);
ListNode<int> * L1 = new ListNode<int>(6);
ListNode<int> * L2 = new ListNode<int>(8);
L1->next = L2;
L->next = L1;
print(removeKFromList(L, 7));
return 0;
}






c++ linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









Jamal

30.2k11115226




30.2k11115226










asked 9 hours ago









Bo Work

383




383












  • That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
    – 200_success
    7 hours ago


















  • That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
    – 200_success
    7 hours ago
















That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
– 200_success
7 hours ago




That's a funny interpretation of "remove". Your function does not edit the list, as I would expect, but rather it constructs a new list with the k nodes filtered out.
– 200_success
7 hours ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207948%2fremove-value-from-linked-list-if-it-is-equal-to-a-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207948%2fremove-value-from-linked-list-if-it-is-equal-to-a-parameter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'