Skip to content

Commit

Permalink
Update 2-insert-a-node-at-the-tail-of-a-linked-list.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
PawarAditi authored Oct 2, 2020
1 parent 77cad2a commit a56abfd
Showing 1 changed file with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
Node* Insert(Node *head, int data)
{
Node *temp = (Node*)malloc(sizeof(struct Node));

if (temp == NULL) {
exit(-1);
}

temp->data = data;
temp->next = NULL;

if (head == NULL) {
head = temp;
} else {
Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = temp;
Node *p = new Node(data);
Node *q;
q = head;

if(head == NULL)
return (p);
while(q->next != NULL){
q = q->next;
}

q->next = p;
return head;
}


0 comments on commit a56abfd

Please sign in to comment.