diff --git a/cpp/data_structures/Insertion in Linked List.cpp b/cpp/data_structures/Insertion in Linked List.cpp new file mode 100644 index 000000000..a94774e71 --- /dev/null +++ b/cpp/data_structures/Insertion in Linked List.cpp @@ -0,0 +1,35 @@ + + +// 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; +} +