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;
}
c++ linked-list
add a comment |
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;
}
c++ linked-list
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 thek
nodes filtered out.
– 200_success
7 hours ago
add a comment |
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;
}
c++ linked-list
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
c++ linked-list
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 thek
nodes filtered out.
– 200_success
7 hours ago
add a comment |
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 thek
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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