diff --git a/algorithms/CPlusPlus/Linked-Lists/singly.cpp b/algorithms/CPlusPlus/Linked-Lists/singly.cpp index 64bd2f93..178cc7e7 100644 --- a/algorithms/CPlusPlus/Linked-Lists/singly.cpp +++ b/algorithms/CPlusPlus/Linked-Lists/singly.cpp @@ -15,10 +15,11 @@ template class SinglyLinkedList { private: Node* head; + Node* tail; public: SinglyLinkedList() - : head(nullptr){} + : head(nullptr),tail(nullptr){} void insertAtHead(T data) { Node *temp = new Node(data); @@ -43,18 +44,15 @@ class SinglyLinkedList { // if head is not pointing to anything then simply point to new node if(this->head == nullptr) { this->head = newNode; + this->tail = newNode; return; } - // if list is not empty, then traverse to the last node using `temp`, then put the address of the new node in the temp->next - // and finally, make the next of newNode = NULL - Node *temp = new Node(); - temp = head; - while(temp->next != NULL){ - temp = temp->next; - } - temp->next = newNode; + // if list is not empty, then put the address of the new node in the tail->next + // and finally, make the next of newNode = NULL and tail will be pointing the the newNode as now it is in the last position. + tail->next = newNode; newNode->next= NULL; + tail=newNode; } T removeAtHead() {