• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help me understand this HackerRank exercise solution  RSS feed

 
Ranch Hand
Posts: 245
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From https://www.hackerrank.com/challenges/30-linked-list/problem

By following tutorials I was extremely close to the solution.  Instead of returning the head, I was returning current .  I'm trying to understand what is going on to learn from this but I'm confused so please can someone help me out?
The full code is below.

Things I don't understand.  Can someone please help?


Print Statements Outputs


  • My while loop say current.next NOT null but when the data value is 3, the current.next value IS null so how did it go into the while loop?
  • Where the data values are 4 and 1, How does current.next now contain a new value?





  • Full Code
  • How does head contain the values which current contains?







  •  
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One of the tutorials I followed by HackerRank was this one https://www.youtube.com/watch?v=njTh_OwMljA&t=137s
    Where I start the video she talks about creating an append method but what she doesn't explain is how the number of nodes available is  first created. 
    She demos at 2:33 a while loop which she says walks through the linked list until we get to the end of the linked list.  How do we know if we are at the end of the linkList?  Well we're not at the end of the linked list as long as there's something after it.  So keep moving until we hit a null.
    https://www.youtube.com/watch?v=njTh_OwMljA&t=153s

    However I don't see where she explains how the linkedList knows how large it needs to be.
     
    Saloon Keeper
    Posts: 4373
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are implementing a simple version of a linked list. A linked list is a kind of collection. Collections tend to expand as needed and don't need to be initialized to a specific size. Your call to new Node(data) causes the list to expand by one node.

    I'd suggest putting a call to your display() method after line 44. That way you can watch the insertion process to see if is behaving the way you expect.
     
    Carey Brown
    Saloon Keeper
    Posts: 4373
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    My while loop say current.next NOT null but when the data value is 3, the current.next value IS null so how did it go into the while loop?


    It is not going in to the while loop. Put a print statement inside the while loop if you're not convinced.
     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    My while loop say current.next NOT null but when the data value is 3, the current.next value IS null so how did it go into the while loop?


    It is not going in to the while loop. Put a print statement inside the while loop if you're not convinced.



    Okay I see that and I understand that.  Thank you
     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:You are implementing a simple version of a linked list. A linked list is a kind of collection. Collections tend to expand as needed and don't need to be initialized to a specific size. Your call to new Node(data) causes the list to expand by one node.

    I'd suggest putting a call to your display() method after line 44. That way you can watch the insertion process to see if is behaving the way you expect.



    I think I see that the head is building up with each loop. 
    2
    2 3
    2 3 4
    2 3 4 1

    Okay I think I understand that.    Where you said how Collections tend to expand as needed .  Does this apply to my question regarding how when data = 4 then 1, how the current.next had a value other than null? I still don't understand that really.  Because why when the data = 3 was it null?
     
    Carey Brown
    Saloon Keeper
    Posts: 4373
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My comment about collection size was in response to this

    Lisa Austin wrote:However I don't see where she explains how the linkedList knows how large it needs to be.

     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:My comment about collection size was in response to this

    Lisa Austin wrote:However I don't see where she explains how the linkedList knows how large it needs to be.



    Ah okay.  Do you have any idea regarding my other question?
    In the print statements where data = 4 then 1 how does current.next have a value?   Both current and next are Node variables right?  next is valued as null on the Node class and there isn't any code which creates a value?

    Also one more question is how does the head variable contain values from current ?  I don't understand how I should have known to return head in this case because I expect to see something like head = current for head to contain the values .
     
    Carey Brown
    Saloon Keeper
    Posts: 4373
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Lisa Austin wrote:In the print statements where data = 4 then 1 how does current.next have a value?

    From

    Both current and next are Node variables right?  next is valued as null on the Node class and there isn't any code which creates a value?


    Also one more question is how does the head variable contain values from current ?

    Head refers to the Node at the beginning of the list. That Node only references one value. To get all the values you must traverse the list.

    I don't understand how I should have known to return head in this case because I expect to see something like head = current for head to contain the values.

    If you set head to current then head refers to the last Node in the list and if you traverse the list from there you won't find any Nodes the precede it.
     
    Marshal
    Posts: 59422
    187
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . . . Collections tend to expand as needed and don't need to be initialized to a specific size. . . . .

    A linked list is slightly unusual amongst collections in that its capacity is always equal to its size. Adding or removing elements causes both to change together.
     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Lisa Austin wrote:In the print statements where data = 4 then 1 how does current.next have a value?

    From

    Both current and next are Node variables right?  next is valued as null on the Node class and there isn't any code which creates a value?


    Also one more question is how does the head variable contain values from current ?

    Head refers to the Node at the beginning of the list. That Node only references one value. To get all the values you must traverse the list.

    I don't understand how I should have known to return head in this case because I expect to see something like head = current for head to contain the values.

    If you set head to current then head refers to the last Node in the list and if you traverse the list from there you won't find any Nodes the precede it.



    Thank You so much!  That makes perfect sense.  My apologies for being late in getting back to respond. I got sick yesterday and hadn't been on to check my message. 
     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Carey Brown wrote:. . . . Collections tend to expand as needed and don't need to be initialized to a specific size. . . . .

    A linked list is slightly unusual amongst collections in that its capacity is always equal to its size. Adding or removing elements causes both to change together.



    Gotcha.  Thank you!
     
    Carey Brown
    Saloon Keeper
    Posts: 4373
    50
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think their choice of the method name "insert()" is unfortunate, it really should be "append()" because you are always adding to the end of the list.
     
    Campbell Ritchie
    Marshal
    Posts: 59422
    187
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What's in a name? When it comes to method names, quite a lot. A clear and descriptive name makes life so much easier for all your users. What's more, there are conventional names for methods/procedures/routines/whatever for different kinds of collections.
     
    Lisa Austin
    Ranch Hand
    Posts: 245
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:I think their choice of the method name "insert()" is unfortunate, it really should be "append()" because you are always adding to the end of the list.



    That makes sense.  I do remember the tutorials I read and watch often used "append" instead of "insert".  Insert sounds like it's adding into something .  Using an example HackerRank used, of Train Cars head then 1-7.  Insert sounds like I'm adding a new train car between 2 and 3 .
     
    I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
    Why should you try IntelliJ IDEA ?
    https://coderanch.com/wiki/696337/IntelliJ-IDEA
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!