Why in Javascript an expected result is an array, although the input is 2 linked lists?


  • 10

    From the task definition, it is clear that we should return a linked list.


  • 1
    S

    @sejoker I am having the same issue.


  • 2

    I figure this out. if you want to create LinkedList you need to use their defined LinkedList like new ListNode(1). I initially used object literals like { val: 1, next: null} and that didn't work. Somehow Leetcode accepts a plain array as an answer instead of linked list. I don't recommend this approach because it requires an extra O(n) loop to do the transformation.

    @Sefton419 you should try using ListNode instead.


  • 4
    O

    @sejoker ListNode keeps returning [Object object] instead of the array-like expected result.


  • 3
    J

    Agreed. Either the description or the excepted result needs to be updated.


  • 0
    Y

    Maybe you use the data structure yourself.I met the problems at first too,but when i delete the structure,the program was ac.


  • 0
    W

    @sejoker Hello.I am a newbie. can you put the code that you was written on this topic?I can not solve it with linked object.Thank.


  • 0
    W

    @yy1820 Hello.I am a newbie. can you put the code that you was written on this topic?I can not solve it with linked object.Thank.


  • 0
    R

    Wait, so is their test suite actually passing in two NodeLists and we have to return it as an array? I am so confused. Or do we also have to transform their array inputs into a NodeList too?


  • 1
    S

    @ocorso The test case is written with array so that you can easily modify the linked list you use for testing. The data structure that the code will be run against (on submission) is based on the definition here:

    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    

    So if you develop on a different editor, make sure you copy the function ListNode and that you create test data using ListNode.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.