Advertisement
Tkap1

Untitled

Jan 22nd, 2024 (edited)
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. set_next(root, new_node) {
  5.     if(root) {
  6.         let current = root;
  7.         while(current.next) {
  8.             current = current.next;
  9.         }
  10.         current.next = new_node;
  11.         return root;
  12.     }
  13.     else {
  14.         return new_node;
  15.     }
  16. }
  17.  
  18. set_child_next(root, new_node) {
  19.     if(root) {
  20.         if(root.child) {
  21.             return set_next(root, new_node);
  22.         }
  23.         else {
  24.             root.child = new_node;
  25.             return root;
  26.         }
  27.     }
  28.     else {
  29.         return new_node;
  30.     }
  31. }
  32.  
  33. let root = null;
  34. root = set_next(root, new Node(...));
  35.  
  36. // or in case of we wanting to set: node.child, node.child.next, node.child.next.next, etc...
  37. root = set_child_next(root, new Node(...));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement