From c4c3b845332a4275c0a49e1746c5bf0f52e5e5aa Mon Sep 17 00:00:00 2001 From: Anushka Dube <75694963+Anushka-3000@users.noreply.github.com> Date: Tue, 5 Oct 2021 01:38:23 +0530 Subject: [PATCH] Insert node at end of singly linked list.cpp --- ...sert node at end of singly linked list.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 cpp/data_structures/Insert node at end of singly linked list.cpp diff --git a/cpp/data_structures/Insert node at end of singly linked list.cpp b/cpp/data_structures/Insert node at end of singly linked list.cpp new file mode 100644 index 000000000..0fa2f8fe1 --- /dev/null +++ b/cpp/data_structures/Insert node at end of singly linked list.cpp @@ -0,0 +1,32 @@ +// Complete the insertNodeAtTail function below. + +/* + * For your reference: + * + * SinglyLinkedListNode { + * int data; + * SinglyLinkedListNode* next; + * }; + * + */ +SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) { + SinglyLinkedListNode* newnode, *temp; + newnode = (SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode)); + temp = (SinglyLinkedListNode*)malloc(sizeof(SinglyLinkedListNode)); + temp->next = head; + newnode->data = data; + newnode->next = NULL; + if(head == NULL) + { + head = newnode; + } + else + { + while(temp->next != NULL) + { + temp = temp->next; + } + temp->next = newnode; + } + return head; +}