Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run leetcode linked list problems in local machine?

How can I run the linked list programs in the local machine? When I run this code in their input box it running but I can't seem to run this program in the local machine.

  function ListNode(val, next) {
      this.val = (val===undefined ? 0 : val)
      this.next = (next===undefined ? null : next)
  }
 
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */

var mergeTwoLists = function (l1, l2) {
  var mergedHead = { val: -1, next: null },
    crt = mergedHead;
  while (l1 && l2) {
    if (l1.val > l2.val) {
      crt.next = l2;
      l2 = l2.next;
    } else {
      crt.next = l1;
      l1 = l1.next;
    }
    crt = crt.next;
  }
  crt.next = l1 || l2;
  return mergedHead.next;
};

mergeTwoLists([1, 2, 4], [1, 3, 4]);
like image 587
Khant Avatar asked Sep 16 '25 08:09

Khant


2 Answers

You could use these two helper functions to convert an array to linked list and vice versa, which is essentially what the LeetCode framework does for you behind the scenes.

const listFromArray = a => a.length ? new ListNode(a[0], listFromArray(a.slice(1)))  
                                    : null;
const arrayFromList = head => head ? [head.val].concat(arrayFromList(head.next)) 
                                   : [];

In your case you can use them like this:

const result = arrayFromList(
    mergeTwoLists(listFromArray([1, 2, 4]), listFromArray([1, 3, 4]))
);
like image 108
trincot Avatar answered Sep 17 '25 21:09

trincot


I was able to run the mergeTwoLists method locally by providing ListNode and LinkedList classes that I generated. As one can see below, the runCase helper function takes two ListNodes (LinkedList heads), merges them, and prints the result. We can create a linked list using toArray with LeetCode test case inputs and convert the result into an array using the fromArray function of the LinkedList.

class ListNode {
  constructor(val=0, next=null) {
      this.val = val;
      this.next = next;             
  }
}

class LinkedList {
  constructor(head = null) {
    this.head = head
  }

  static fromArray(array) {
    const linkedList = new LinkedList();

    for (let i = array.length - 1; i >= 0; i--) {
      const node = new ListNode(array[i]);
      node.next = linkedList.head;
      linkedList.head = node;
    }

    return linkedList;
  }

  toArray() {
    const array = [];
    let current = this.head;

    while (current) {
      array.push(current.val);
      current = current.next;
    }

    return array;
  }
}

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 *
 * @param {ListNode} node1
 * @param {ListNode} node2
 * @return {ListNode}
 */
var mergeTwoLists = function(node1, node2) {
  let loco = new ListNode();
  let current = loco;

  while (node1 && node2) {
    if (node1.val > node2.val) {
      current.next = node2;
      node2 = node2.next;
    } else {
      current.next = node1;
      node1 = node1.next;
    }
    current = current.next;
  }
  current.next = node1 || node2;

  return loco.next;
};

const runCase = (array1, array2) => {
  const node1 = LinkedList.fromArray(array1).head;
  const node2 = LinkedList.fromArray(array2).head;

  const result = mergeTwoLists(node1, node2);
  const linkedList = new LinkedList(result);
  console.log(linkedList.toArray());
}

runCase([1, 2, 4], [1, 3, 4]); // [1,1,2,3,4,4]
runCase([], []); // []
runCase([], [0]); // [0]

Reference

  • 21. Merge Two Sorted Lists by LeetCode
  • How to Implement a Linked List in JavaScript by Sarah Chima Atuonwu
like image 32
serdarsen Avatar answered Sep 17 '25 21:09

serdarsen