Input Format
You have to complete the Node* Insert(Node* head, int data) method. It takes two arguments: the head of the linked list and the integer to insert. You should not read any input from the stdin/console.
You have to complete the Node* Insert(Node* head, int data) method. It takes two arguments: the head of the linked list and the integer to insert. You should not read any input from the stdin/console.
Output Format
Insert the new node at the tail and just return the head of the updated linked list. Do not print anything to stdout/console.
Insert the new node at the tail and just return the head of the updated linked list. Do not print anything to stdout/console.
Sample Input
NULL, data =
--> NULL, data =
--> NULL, data =
Sample Output
2 -->NULL
2 --> 3 --> NULL
Explanation
1. We have an empty list, and we insert .
2. We start with a in the tail. When is inserted, then becomes the tail.
1. We have an empty list, and we insert .
2. We start with a in the tail. When is inserted, then becomes the tail.
/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
0
{
int data;
struct Node *next;
}
*/
Node* Insert(Node *head,int data)
{
struct Node *newnode,*temp;
newnode=new Node;
newnode->next=NULL;
newnode->data=data;
temp=head;
if(temp!=NULL)
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
return(head);
} // Complete this method
else{
head=newnode;
return(head);
}
}