Insert a node at a specific position in a linked list

  • + 0 comments
    function insertNodeAtPosition($llist, $data, $position) {
        if($position == 0) {
            $node = new SinglyLinkedListNode($data);
            $node->next = $llist;
            return $node;
        }
        $llist->next = insertNodeAtPosition($llist->next, $data, $position - 1);
        return $llist;
    }