Problem Statement
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node. The given head pointer may be null, meaning that the initial list is empty.
Input Format
You have to complete the
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
Insert the new node at the tail and just
return
the head of the updated linked list. Do notprint anything to stdout/console.
Sample Input
NULL, data = 2
2 --> NULL, data = 3
Sample Output
2 -->NULL
2 --> 3 --> NULL
Explanation
1. We have an empty list, and we insert2 .
2. We start with a2 in the tail. When 3 is inserted, 3 then becomes the tail.
1. We have an empty list, and we insert
2. We start with a
Video lesson
Copyright © 2015 HackerRank.
All Rights Reserved
All Rights Reserved
/*
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
{
int data;
struct Node *next;
}
*/
Node* Insert(Node *head,int data)
{
// Complete this method
Node *newn;
newn = (struct Node *)malloc(sizeof(struct Node));
newn -> data = data;
newn -> next = NULL;
if(head == NULL)
{
head = newn;
}
else
{
Node *tmp;
for(tmp = head ; tmp -> next != NULL ; tmp = tmp -> next);
tmp -> next = newn ;
}
return head;
}
No comments:
Post a Comment