Calling function outside LeetCode

Below is a Solution to LeetCode exercise Single LinkedList isPalindrome. I want to do it outside of LeetCode, meaning I want to call it (ie: console.log([2,3,3,4]) which should return true. Solution is not working for me. And I wonder if I need to provide a ListNode function for “val”. As usual, I can follow a tutorial on LinkedLists somewhat fine but when I need to re-arrange the code to make it work for my own purposes, I struggled.

var isPalindrome = function(head) {
    const array = [];
    while(head){
        array.push(head.val);
        head = head.next;
    }
    
    let start = 0;
    let end = array.length - 1;
    while(start < end){
        if(array[start] !== array[end]){
            return false;
        }
        start++;
        end--;
    }
    return true;
};

You are turning a linked list into an array? Where is the linked list itself coming from? This code seems incomplete.

well, that is part of my question. LeetCode provides all the plumbing for you. So I want to do it outside of LeetCode and make it work so I learn. Below is code I have for a simple Linked List and ListNode. What parts of this code do I need to make it work in my previous example?

class ListNode {
    // this is the data - Note the constructor
    constructor(data) {
        this.data = data
        this.next = null                
    }
}

class LinkedList {
    // head is initialize to null Note the constructor
    constructor(head = null) {
        this.head = head
    }
}

I would like to resolve this problem. do you need more info than what I provided to help me out?

Yeah, I mean using the function should be easy. The only sticking issue is having a linked list with which to test it. Do you understand how a linked list works, how to build one?

At the risk of giving away too much… but this isn’t an FCC challenge and you aren’t asking for the answer to the algorithm…

Can you run it like this:

const ll1 = { val: 'a', next: { val: 'b', next: { val: 'a', next: null } } }
const ll2 = { val: 'a', next: { val: 'b', next: { val: 'c', next: null } } }

console.log(isPalindrome(ll1))
// true
console.log(isPalindrome(ll2))
// false

Note that this is based on what you have in the code, with the info stored in a a val prop, which is different than what you have in your LL constructors.

yes, it’s a data structure. the elements are not stored in contiguous memory locations but rather use pointers to connect nodes. Head is the beginning and when it returns null it ends. But my problem is not understanding it but rather implementing it.

I don’t see where you have implemented any logic for adding a node to your linked list. What have you tried for this? Even some pseudocode would give us a starting place.

I want to stress this point. Coding is relatively easy with a self contained tutorial but it becomes a learning experience when you use it outside the tutorial, say, in a different exercise.

Ok… I disagree that learning coding is relatively easy in any situation, but that is beside the point.

In order for us fix problems, we need to interact. One big problem with your code is that you have no way to add nodes to your linked list… so… what have you tried so far to add nodes to your linked list… even some pseudocode or a description of how you think you would do this would give us a starting place

We need to be able to start the conversation somewhere. Anything you can tell me about what you understand about adding nodes to a linked list would help.

1 Like

Yeah, perhaps look at some youtube videos about implementing linked lists in JS. I’m sure there are plenty of them, some of them from FCC.

Hi Kevin, your sample code made me understand what was needed for the test driver. Code needs a few adjustments but is now passing in LeetCode template as well as my own javascript editor.

function ListNode(val, next) {
     this.val = (val===undefined ? 0 : val)
     this.next = (next===undefined ? null : next)
}

var addTwoNumbers = function(l1, l2) {
    
    let list = new ListNode(0);
    let currVal = list;
   
    let sum = 0
    let carry = 0;
    while (l1 !== null || l2 !== null || sum > 0 ) {
                if (l1 !== null) {
                  console.log(l1.val)
                  sum += l1.val
                  l1 = l1.next;
               } 
               if (l2 !== null){        
                   sum += l2.val 
                   l2 = l2.next;
               }
               carry = Math.floor(sum / 10)
               sum = sum % 10;
               currVal.next = new ListNode(sum);
               currVal = currVal.next;
               sum = carry;
               carry = 0;
    }
    console.log(list.next) 
}
// test driver
const l1 = { val: 2, next: { val: 4, next: { val: 3, next: null } } }
const l2 = { val: 5, next: { val: 6, next: { val: 9, next: null } } }
console.log(addTwoNumbers(l1, l2))
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.