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 3 of 3

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,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 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.

  3. #3
    Junior Member
    Join Date
    Jun 2022
    Location
    GB
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    you are given two linked lists representing two non-negative integers in reverse order. You need to add the two numbers and return the result as a linked list in reverse order. For example, if the input is (2 -> 4 -> 3) and (5 -> 6 -> 4), the output should be (7 -> 0 -> 8), which represents the sum of 342 and 465 in reverse order.

    You can Try this:

    **
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode curr = dummy;
    int carry = 0;

    while (l1 != null || l2 != null || carry != 0) {
    int val1 = (l1 != null) ? l1.val : 0;
    int val2 = (l2 != null) ? l2.val : 0;
    int sum = val1 + val2 + carry;
    carry = sum / 10;
    curr.next = new ListNode(sum % 10);
    curr = curr.next;
    l1 = (l1 != null) ? l1.next : null;
    l2 = (l2 != null) ? l2.next : null;
    }

    return dummy.next;
    }
    }

    Found this solution here(https://www.techgeekbuzz.com/blog/co...iew-questions/)
    Last edited by lokeshjoshi; March 30th, 2023 at 01:30 AM.

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