Confused why this answers gives me "time limit exceeded"


  • 0
    A

    Hey guys. First time posting a question here. I'm trying to brush on on my linked list knowledge. Pretty rusty at it. I've been stuck on this question for a couple days now and I've officially run out of ideas as to why it's not passing. When I run the code it returns the correct answers but when I try to submit it I get "time limit exceeded". Any ideas on what I'm missing here? Thank you in advance.

    /**
    * Definition for singly-linked list.
    * function ListNode(val) {
    * this.val = val;
    * this.next = null;
    * }
    /
    /
    *
    * @param {ListNode} head
    * @param {number} val
    * @return {ListNode}
    */
    var removeElements = function(head, val) {
    if(!head){
    return null;
    }

        while(head){
            if(head.next === null){
                if(head.val === val){
                    return [];
                } else {
                    return head;
                }
            } else if (head.val === val){
                head.val = head.next.val;
                head.next = head.next.next;
            } else {
                head.next = head.next;
            }
        }
            
        return head;
    };

  • 0

    Have you run you code on your local machine?? You have a while loop using head as stop condition but I didn't find that you change the head in the loop, maybe it is a endless loop??

    EDIT: maybe you make a mistake in the last else: head=head.next ???


Log in to reply
 

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