Print the Elements of a Linked List


Problem Statement :


This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print.

Function Description:

Complete the printLinkedList function in the editor below.

printLinkedList has the following parameter(s):
      1.SinglyLinkedListNode head: a reference to the head of the list

Print:
     1. For each node, print its  value on a new line (console.log in Javascript).


Input Format:

The first line of input contains n, the number of elements in the linked list.
The next n lines contain one element each, the data values for each node.

Note: Do not read any input from stdin/console. Complete the printLinkedList function in the editor below.


Constraints:
    1. 1<=n<=1000
    2. 1<=list[i]<=1000



Solution :



title-img


                            Solution in C :

In C:
//In hacker rank as the solution the program is too big 
//all u need is to code this function.

void printLinkedList(SinglyLinkedListNode* head) {

SinglyLinkedListNode* ptr=head;
    while(ptr!=NULL)
    {
        printf("%d\n",ptr->data);
        ptr=ptr->next;
    }

}







In C++:
//The rest of the code is already coded.
//As the code is too big all you need is complete the following part.

void printLinkedList(SinglyLinkedListNode* head) {

     while(head!=NULL)
     {
         cout << head->data << "\n";
         head = head->next;
     }
}









In Java:
//The following part is all you to complete, the rest is
//already present in hackerrank 

 static void printLinkedList(SinglyLinkedListNode head) {
        while(head != null) { System.out.println(head.data); head = head.next;}

    }






In Python 3:

# The following part of code is all you need to 
# complete the challenge in hacker rank

def printLinkedList(head):
    temp = head
    while (temp.next != None):
        print(temp.data)
        temp = temp.next
    print(temp.data)
                        








View More Similar Problems

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →