Linked List Interview Code methods, runtime, and edge cases refactored
$begingroup$
In my previous post I had a number of steps given to me from the accepted answer on how I can write more production ready code. In this post I want this code to be reviewed as near production code I'd write in an interview. I'd like to know how well optimized my code is now from this improvements I've made to it. If there are other optimizations or c++11 features I could be using here please share that as well.
I also included comments on worst case run time complexity for my member functions, I'd like some feedback on this and my space complexity too.
#include <iostream>
#include <string>
class LinkedList
{
private:
struct Node
{
int data;
Node* next;
Node(int data_) : data(data_), next(nullptr) { }
};
Node* _head;
Node* _tail;
public:
LinkedList() : _head(nullptr) { }
LinkedList(int data) { // O(1)
Node* n = new Node(data);
_head = n;
_tail = n;
}
~LinkedList() { // O(n)
Node* current = _head;
Node* n;
while(current) {
n = current;
current = current->next;
delete n;
}
_head = nullptr;
}
void append(int new_data) { // O(1)
auto n = new Node(new_data);
if(!_head) {
_head = n;
_tail = n;
}
else {
_tail->next = new Node(new_data);
_tail = _tail->next;
}
}
void remove(int target_data) { // O(n)
if(!_head) { return; }
Node* previous = nullptr;
auto current = _head;
for(; current->data != target_data; current = current->next) {
if(!current->next) {
return;
}
previous = current;
}
if(!current->next) {
if(previous) {
delete current;
previous->next = nullptr;
}
else {
delete current;
_head = nullptr;
}
}
else {
auto dangling_ptr = current->next;
current->data = current->next->data;
current->next = current->next->next;
delete dangling_ptr;
}
}
friend std::ostream& operator<<(std::ostream& os, const LinkedList& linked_list) { // O(n)
Node* current = linked_list._head;
if(!current) {
os << "Linked List is empty" << std::endl;
return os;
}
else {
for(; current; current = current->next) {
os << '(' << std::to_string(current->data) << ")->";
}
os << "nullptr";
return os;
}
}
};
int main()
{
LinkedList linked_List(0);
linked_List.append(1);
linked_List.append(2);
linked_List.append(3);
linked_List.append(4);
std::cout << linked_List << std::endl;
linked_List.remove(0);
linked_List.remove(4);
linked_List.remove(2);
std::cout << linked_List << std::endl;
linked_List.remove(3);
linked_List.remove(1);
std::cout << linked_List << std::endl;
LinkedList linked_List_two;
linked_List_two.append(1);
linked_List_two.append(2);
linked_List_two.append(3);
linked_List_two.append(4);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(0);
linked_List_two.remove(4);
linked_List_two.remove(2);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(3);
linked_List_two.remove(1);
std::cout << linked_List_two << std::endl;
}
c++ c++11 linked-list pointers
$endgroup$
add a comment |
$begingroup$
In my previous post I had a number of steps given to me from the accepted answer on how I can write more production ready code. In this post I want this code to be reviewed as near production code I'd write in an interview. I'd like to know how well optimized my code is now from this improvements I've made to it. If there are other optimizations or c++11 features I could be using here please share that as well.
I also included comments on worst case run time complexity for my member functions, I'd like some feedback on this and my space complexity too.
#include <iostream>
#include <string>
class LinkedList
{
private:
struct Node
{
int data;
Node* next;
Node(int data_) : data(data_), next(nullptr) { }
};
Node* _head;
Node* _tail;
public:
LinkedList() : _head(nullptr) { }
LinkedList(int data) { // O(1)
Node* n = new Node(data);
_head = n;
_tail = n;
}
~LinkedList() { // O(n)
Node* current = _head;
Node* n;
while(current) {
n = current;
current = current->next;
delete n;
}
_head = nullptr;
}
void append(int new_data) { // O(1)
auto n = new Node(new_data);
if(!_head) {
_head = n;
_tail = n;
}
else {
_tail->next = new Node(new_data);
_tail = _tail->next;
}
}
void remove(int target_data) { // O(n)
if(!_head) { return; }
Node* previous = nullptr;
auto current = _head;
for(; current->data != target_data; current = current->next) {
if(!current->next) {
return;
}
previous = current;
}
if(!current->next) {
if(previous) {
delete current;
previous->next = nullptr;
}
else {
delete current;
_head = nullptr;
}
}
else {
auto dangling_ptr = current->next;
current->data = current->next->data;
current->next = current->next->next;
delete dangling_ptr;
}
}
friend std::ostream& operator<<(std::ostream& os, const LinkedList& linked_list) { // O(n)
Node* current = linked_list._head;
if(!current) {
os << "Linked List is empty" << std::endl;
return os;
}
else {
for(; current; current = current->next) {
os << '(' << std::to_string(current->data) << ")->";
}
os << "nullptr";
return os;
}
}
};
int main()
{
LinkedList linked_List(0);
linked_List.append(1);
linked_List.append(2);
linked_List.append(3);
linked_List.append(4);
std::cout << linked_List << std::endl;
linked_List.remove(0);
linked_List.remove(4);
linked_List.remove(2);
std::cout << linked_List << std::endl;
linked_List.remove(3);
linked_List.remove(1);
std::cout << linked_List << std::endl;
LinkedList linked_List_two;
linked_List_two.append(1);
linked_List_two.append(2);
linked_List_two.append(3);
linked_List_two.append(4);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(0);
linked_List_two.remove(4);
linked_List_two.remove(2);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(3);
linked_List_two.remove(1);
std::cout << linked_List_two << std::endl;
}
c++ c++11 linked-list pointers
$endgroup$
add a comment |
$begingroup$
In my previous post I had a number of steps given to me from the accepted answer on how I can write more production ready code. In this post I want this code to be reviewed as near production code I'd write in an interview. I'd like to know how well optimized my code is now from this improvements I've made to it. If there are other optimizations or c++11 features I could be using here please share that as well.
I also included comments on worst case run time complexity for my member functions, I'd like some feedback on this and my space complexity too.
#include <iostream>
#include <string>
class LinkedList
{
private:
struct Node
{
int data;
Node* next;
Node(int data_) : data(data_), next(nullptr) { }
};
Node* _head;
Node* _tail;
public:
LinkedList() : _head(nullptr) { }
LinkedList(int data) { // O(1)
Node* n = new Node(data);
_head = n;
_tail = n;
}
~LinkedList() { // O(n)
Node* current = _head;
Node* n;
while(current) {
n = current;
current = current->next;
delete n;
}
_head = nullptr;
}
void append(int new_data) { // O(1)
auto n = new Node(new_data);
if(!_head) {
_head = n;
_tail = n;
}
else {
_tail->next = new Node(new_data);
_tail = _tail->next;
}
}
void remove(int target_data) { // O(n)
if(!_head) { return; }
Node* previous = nullptr;
auto current = _head;
for(; current->data != target_data; current = current->next) {
if(!current->next) {
return;
}
previous = current;
}
if(!current->next) {
if(previous) {
delete current;
previous->next = nullptr;
}
else {
delete current;
_head = nullptr;
}
}
else {
auto dangling_ptr = current->next;
current->data = current->next->data;
current->next = current->next->next;
delete dangling_ptr;
}
}
friend std::ostream& operator<<(std::ostream& os, const LinkedList& linked_list) { // O(n)
Node* current = linked_list._head;
if(!current) {
os << "Linked List is empty" << std::endl;
return os;
}
else {
for(; current; current = current->next) {
os << '(' << std::to_string(current->data) << ")->";
}
os << "nullptr";
return os;
}
}
};
int main()
{
LinkedList linked_List(0);
linked_List.append(1);
linked_List.append(2);
linked_List.append(3);
linked_List.append(4);
std::cout << linked_List << std::endl;
linked_List.remove(0);
linked_List.remove(4);
linked_List.remove(2);
std::cout << linked_List << std::endl;
linked_List.remove(3);
linked_List.remove(1);
std::cout << linked_List << std::endl;
LinkedList linked_List_two;
linked_List_two.append(1);
linked_List_two.append(2);
linked_List_two.append(3);
linked_List_two.append(4);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(0);
linked_List_two.remove(4);
linked_List_two.remove(2);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(3);
linked_List_two.remove(1);
std::cout << linked_List_two << std::endl;
}
c++ c++11 linked-list pointers
$endgroup$
In my previous post I had a number of steps given to me from the accepted answer on how I can write more production ready code. In this post I want this code to be reviewed as near production code I'd write in an interview. I'd like to know how well optimized my code is now from this improvements I've made to it. If there are other optimizations or c++11 features I could be using here please share that as well.
I also included comments on worst case run time complexity for my member functions, I'd like some feedback on this and my space complexity too.
#include <iostream>
#include <string>
class LinkedList
{
private:
struct Node
{
int data;
Node* next;
Node(int data_) : data(data_), next(nullptr) { }
};
Node* _head;
Node* _tail;
public:
LinkedList() : _head(nullptr) { }
LinkedList(int data) { // O(1)
Node* n = new Node(data);
_head = n;
_tail = n;
}
~LinkedList() { // O(n)
Node* current = _head;
Node* n;
while(current) {
n = current;
current = current->next;
delete n;
}
_head = nullptr;
}
void append(int new_data) { // O(1)
auto n = new Node(new_data);
if(!_head) {
_head = n;
_tail = n;
}
else {
_tail->next = new Node(new_data);
_tail = _tail->next;
}
}
void remove(int target_data) { // O(n)
if(!_head) { return; }
Node* previous = nullptr;
auto current = _head;
for(; current->data != target_data; current = current->next) {
if(!current->next) {
return;
}
previous = current;
}
if(!current->next) {
if(previous) {
delete current;
previous->next = nullptr;
}
else {
delete current;
_head = nullptr;
}
}
else {
auto dangling_ptr = current->next;
current->data = current->next->data;
current->next = current->next->next;
delete dangling_ptr;
}
}
friend std::ostream& operator<<(std::ostream& os, const LinkedList& linked_list) { // O(n)
Node* current = linked_list._head;
if(!current) {
os << "Linked List is empty" << std::endl;
return os;
}
else {
for(; current; current = current->next) {
os << '(' << std::to_string(current->data) << ")->";
}
os << "nullptr";
return os;
}
}
};
int main()
{
LinkedList linked_List(0);
linked_List.append(1);
linked_List.append(2);
linked_List.append(3);
linked_List.append(4);
std::cout << linked_List << std::endl;
linked_List.remove(0);
linked_List.remove(4);
linked_List.remove(2);
std::cout << linked_List << std::endl;
linked_List.remove(3);
linked_List.remove(1);
std::cout << linked_List << std::endl;
LinkedList linked_List_two;
linked_List_two.append(1);
linked_List_two.append(2);
linked_List_two.append(3);
linked_List_two.append(4);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(0);
linked_List_two.remove(4);
linked_List_two.remove(2);
std::cout << linked_List_two << std::endl;
linked_List_two.remove(3);
linked_List_two.remove(1);
std::cout << linked_List_two << std::endl;
}
c++ c++11 linked-list pointers
c++ c++11 linked-list pointers
edited 2 hours ago
greg
asked 2 hours ago
greggreg
30418
30418
add a comment |
add a comment |
0
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f215155%2flinked-list-interview-code-methods-runtime-and-edge-cases-refactored%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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%2f215155%2flinked-list-interview-code-methods-runtime-and-edge-cases-refactored%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