Home > Software design >  LinkdeList hackerrank challenge - Print all Nodes
LinkdeList hackerrank challenge - Print all Nodes

Time:06-11

I am stuck on this supposedly very simple data structure question. The link to the problem is here: https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem?isFullScreen=true

Can someone point out how to approach this correctly?

import java.io.*;
import java.util.*;

public class Solution {
    
    private class Node{
        int data;
        Node next;
        private Node (int data){
        this.data = data;
        this.next = null;
        }
    }
    
    static void setHead(LinkedList<Integer> lList){
        Node head;
        if (lList.peek() != null){
            head = lList.peek();
            return;
        }else{
            return;
        }
    }
    
    static void input(LinkedList<Integer> lList){
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int i = 0;
        while (i < count){
            int data = scanner.nextInt();
            lList.add(data);
            i  ;
            }
        scanner.close();
        return;
    }
    static void printLinkedList(LinkedList<Integer> lList){
        while(lList.head != null){
            Sdtout.println(lList.head.data);
            this.head = head.next;
        }
        return;
    }
    

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        LinkedList<Integer> n1 = new LinkedList<Integer>();
        setHead(n1);
        input(n1);
        printLinkedList(n1);
    }
}

Errors: (I am very confused how to set the head node giving a linkedlist)

Solution.java:19: error: incompatible types: Integer cannot be converted to Solution.Node
            head = lList.peek();
                             ^
Solution.java:39: error: cannot find symbol
        while(lList.head != null){
                   ^
  symbol:   variable head
  location: variable lList of type LinkedList<Integer>
Solution.java:40: error: cannot find symbol
            Sdtout.println(lList.head.data);
                                ^
  symbol:   variable head
  location: variable lList of type LinkedList<Integer>
Solution.java:40: error: cannot find symbol
            Sdtout.println(lList.head.data);
            ^
  symbol:   variable Sdtout
  location: class Solution
Solution.java:41: error: non-static variable this cannot be referenced from a static context
            this.head = head.next;
            ^
Solution.java:41: error: cannot find symbol
            this.head = head.next;
                ^
  symbol: variable head
Solution.java:41: error: cannot find symbol
            this.head = head.next;
                        ^
  symbol:   variable head
  location: class Solution
7 errors

CodePudding user response:

You don't need to use the LinkedList class built-in in the JDK. Instead, you have to utilize a very basic custom implementation of the Singly linked list data structure.

If you choose the language level Java 8, then almost ready to go implementation will be provided for you.

You only need to implement a single method that does precisely what required according to the challenge description, and nothing more:

public static void printLinkedList(SinglyLinkedListNode head) {
    SinglyLinkedListNode cur = head;
    while (cur != null) { // while current node exists
        System.out.println(cur.data); // print its data
        cur = cur.next;               // and reassign the current node to its next node
    }
}
  • Related