Hacker Rank Problem -Insert a node at a specific position in a linked list in c++

You’re given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. Create a new node with the given integer, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty. As an example, if your list starts as and you want to insert a node at position with , your new list should be Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list. insertNodeAtPosition has the following parameters:

  • head: a SinglyLinkedListNode pointer to the head of the list

  • data: an integer value to insert as data in your new node

  • position: an integer position to insert the new node, zero based indexing


Input Format The first line contains an integer , the number of elements in the linked list. Each of the next lines contains an integer SinglyLinkedListNode[i].data. The next line contains an integer denoting the data of the node that is to be inserted. The last line contains an integer .

Constraints

  • , where is the element of the linked list.

  • .


Output Format Return a reference to the list head. Locked code prints the list for you.

Sample Input 3

16

13

7

1 2

Sample Output 16 13 1 7

Explanation The initial linked list is 16 13 7. We have to insert at the position which currently has in it. The updated linked list will be 16 13 1 7



c++ code



#include <bits/stdc++.h> using namespace std; class SinglyLinkedListNode { public: int data; SinglyLinkedListNode *next; SinglyLinkedListNode(int node_data) { this->data = node_data; this->next = nullptr; } }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } void insert_node(int node_data) { SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data); if (!this->head) { this->head = node; } else { this->tail->next = node; } this->tail = node; } }; void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { while (node) { fout << node->data; node = node->next; if (node) { fout << sep; } } } void free_singly_linked_list(SinglyLinkedListNode* node) { while (node) { SinglyLinkedListNode* temp = node; node = node->next; free(temp); } } // Complete the insertNodeAtPosition function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* head, int data, int position) { SinglyLinkedListNode * temp = new SinglyLinkedListNode(data); if (head==NULL) { temp->next=NULL; head = temp; return head; } SinglyLinkedListNode *prev =head; SinglyLinkedListNode * go = head->next; for(int i =1 ; i<position;i++) { prev=prev->next; go = go->next; } prev->next = temp; temp->next=go; return head; } int main() { ofstream fout(getenv("OUTPUT_PATH")); SinglyLinkedList* llist = new SinglyLinkedList(); int llist_count; cin >> llist_count; cin.ignore(numeric_limits<streamsize>::max(), '\n'); for (int i = 0; i < llist_count; i++) { int llist_item; cin >> llist_item; cin.ignore(numeric_limits<streamsize>::max(), '\n'); llist->insert_node(llist_item); } int data; cin >> data; cin.ignore(numeric_limits<streamsize>::max(), '\n'); int position; cin >> position; cin.ignore(numeric_limits<streamsize>::max(), '\n'); SinglyLinkedListNode* llist_head = insertNodeAtPosition(llist->head, data, position); print_singly_linked_list(llist_head, " ", fout); fout << "\n"; free_singly_linked_list(llist_head); fout.close(); return 0; }

32 views0 comments

Recent Posts

See All

How to create a Web View In swift Ios

import UIKit // import webkit framework import WebKit class ViewController: UIViewController, WKNavigationDelegate { // view controller inherit from WkNavigation Delegate // create a object of Wk

How to work with an api in Swift

Create a URl let url = URL(string: " your_url")! Create a URL Session let datatask = URLSession.shared.dataTask(with: url) { (data, response, error) in if let data = data { print(data) }else { p

 

Follow

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Contact

Address

VILLAGE-HiRAPUR KALAN
POST-KARANWAS DIBAI

©2020 by ThE RoYaL LaKsHyA.