Sort by

recency

|

975 Discussions

|

  • + 0 comments

    Yet another C# broken template (5th now?)

    remove result class, but keep its guts. make method non-public.

  • + 0 comments

    solution with JS

    function getNode(llist, positionFromTail) {
        // Write your code here
        var currentNode = llist;
        var len = 0
        var arr = [];
        if(llist==null){
            return false;
        }
        while(currentNode){
            len++;
            arr.unshift(currentNode.data)
            currentNode = currentNode.next
        }
            if(positionFromTail>len || positionFromTail<0){
                return false
            }
    
        return arr[positionFromTail]
        
    }
    
  • + 1 comment
    def atNodeFromTail(head,position):
        cur = head
        stack = []
        while cur != None:
            stack.append(cur.data)
            cur = cur.next
        return stack[-position-1]
    

    Simple Solution with O(N) complexity

  • + 0 comments

    typescript, just recursion, not creating a stack or a dict of the visited nodes

    function traverse (llist: SinglyLinkedListNode, positionFromTail: number, found: { value: number}): number {
        if(llist.next !== null){
            const pos = 1 + traverse(llist.next, positionFromTail, found)
            if (pos === positionFromTail){
                found.value = llist.data
            }
            return pos
        }
        if (positionFromTail === 0){
            found.value = llist.data
        }
        return 0
    }
    
    function getNode(llist: SinglyLinkedListNode, positionFromTail: number): number {
        const found = {  value: 0}
        traverse(llist, positionFromTail, found)
        return found.value
    }
    
  • + 0 comments
        Stack<Integer> stack = new Stack<>();
    
    while(llist != null) {
        stack.push(llist.data);
        llist = llist.next;
    }
    
    for (int i = 0; i < positionFromTail; i++) {
        stack.pop();
    }
    
    // The top of the stack is now the desired node
    return stack.pop();