Insert a Node at the Tail of a Linked List HackerRank Solution


Insert a Node at the Tail of a Linked List HackerRank Solution
Source : https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list



Source : https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list


Solution


// Karthikalapati.blogspot.com
/*
Insert Node at the end of a linked list
Node is defined as
class Node {
int data;
Node next;
}
*/
Node Insert(Node head, int data) {
Node n = new Node();
n.data = data;
n.next = null;
if (head == null) {
return n;
} else {
Node curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = n;
return head;
}
}

3 comments:

  1. Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Buy Whisky online cheap

    ReplyDelete
  2. thank you for your interesting infomation. naija news

    ReplyDelete
  3. thanks for this usefull article, waiting for this article like this again. 토토사이트

    ReplyDelete