简体   繁体   中英

java- understanding LinkedList interview questions with nodes

I am currently reading cracking the coding interview and looking at questions on leetcode and have encountered the same confusion in both places. Specifically with LinkedList problems, which often involve using nodes and creating custom class implementations of Linked Lists. Now I understand what a LinkedList is and how each element is called a "node", etc. But this level seems to be too low level when actually working with LinkedList java data structure and is causing me confusion.

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 leetcode 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.

After reading this problem I went to my whiteboard and coded out a solution. As you may imagine, when I went to compare my answer to the solution I was immediately shocked as my code differed from the solution in the 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)

Please explain what I appear to be missing. Why doesn't the solution receive an actual LinkedList data structure? The question clearly states to return a LinkedList, yet it returns a custom implemented ListNode. I seem to be missing a basic understanding of what was asked.

Question on leetcode: https://leetcode.com/problems/add-two-numbers/description/

This has nothing to do with the Java built-in LinkedList , except in concept.

One of the things they teach in Programming 101 (or whatever it's called), is how linked lists in general work.

They usually start with singly-linked lists, as illustrated on Wikipedia , and will then cover other types of linked lists, such as doubly-linked lists (which is how the built-in LinkedList is implemented). See Wikipedia article for full list of linked list types (section 3).

In a singly-linked list, the list is made up of nodes, each with a value and a reference to the next node. In a full list implementation, the nodes are internal to a List class (like is done by LinkedList ), but for simple/early implementations, only the ListNode class exists, and a list is represented by the reference to the "head" / first node of the list.

It is this overly simple type of list the questions are working with. If you want to "crack the coding interview" for this low-level of programming, you should study how linked lists work internally.

You could read that Wikipedia article, or search the web for material about linked lists in Java.

While @Andreas answer is good, and more at a higher level, my misunderstanding seems to be that I am not familiar with how leetcode works and the device I viewed the question on made the "Submit Solution" part not easily noticeable. My mistake was thinking this was a stand-alone question. I didn't notice the following at the bottom of the page which implies how to answer the question:

/**
 * 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) {

    }
}

In the interview, the interviewer actually won't tell you what the function signature is like. You will have to decide the function signature based on the interview question (Of course the interviewee should discuss with the interviewer before writing code).

For this problem, LinkedList is a black box with only a few APIs to use. So looping the list will actually take O(n^2). Since for each element, you will have to start from the head or tail and move step by step to the desired position. However, with ListNode there is much more freedom, so you can loop the list at O(n), which is also the optimized time complexity for this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM