Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Understanding nodes and how they relate to LinkedList interview questions.

  1. #1
    Junior Member
    Join Date
    Oct 2022
    Location
    Pune
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Understanding nodes and how they relate to LinkedList interview questions.

    I have been trying to understand LinkedList problems when it comes to Java but I am confused about how the Collection List/LinkedList API plays into them. I understand the concept of nodes and what a LinkedList is, but it feels like the exercises I am doing are too low-level and not related to the Java Collections List/LinkedList API. When I search the API for "node", I don't get any results.

    Does any of this actually have to do with java Collections List/ LinkedList api? it doesn't seem so. For example if I search the LinkedList api for "node", I don't even get a single hit.

    Take the following question:
    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Example

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

    Output: 7 -> 0 -> 8

    Explanation: 342 + 465 = 807.


    When I read the question, I wrote out the code for a solution on my whiteboard. To my surprise, the answer I had written was already different from the solution in the very first line!

    I wrote the following:

    ```
    public LinkedList<Integer> addLinkedLists(LinkedList<Integer> l1, LinkedList<Integer> l2)
    ```

    and the solution had the following:

    ```
    public ListNode addTwoNumbers(ListNode l1, ListNode l2)
    ```

    Question resource: https://www.interviewbit.com/java-interview-questions/
    Can you provide more details about the solution you are referring to? What is the expected output of the solution? What implementation of LinkedList is the solution supposed to use?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Understanding nodes and how they relate to LinkedList interview questions.

    return it as a linked list.
    the solution had the following:
    public ListNode addTwoNumbers(ListNode l1, ListNode l2)
    I do not understand:
    That method declaration does not return a linked list.

    That method looks like it should be a private method that was used by the top level method that is adding the contents of the two linked lists. However I do not see how to handle the "carry" to the next node. EG 9 + 9 is 8 carry 1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: July 20th, 2018, 01:10 AM
  2. Java interview questions
    By Varuna2014 in forum The Cafe
    Replies: 3
    Last Post: February 14th, 2018, 03:57 AM
  3. eclipse interview questions and answers
    By saggammahesh in forum Java IDEs
    Replies: 1
    Last Post: April 17th, 2012, 02:07 AM
  4. JAVA INTERVIEW QUESTIONS
    By kanchana1 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 8th, 2011, 08:23 PM
  5. Resultset for relate table
    By whiga in forum JDBC & Databases
    Replies: 1
    Last Post: May 16th, 2011, 01:04 PM