unshift(data){constnewHead=newNode(data,this.head);this.length++;this.head=newHead;}// ✔ Adds new node to start of list by correctly setting head and updating length.// ✔ Does not overwrite old head.
Getting First Node (Head)
getFirst(){returnthis.head;}// ✔ Returns the first node in linked list.
Getting Last Node (Tail)
getLast(){letcurrentNode=this.head;while(currentNode&¤tNode.next){currentNode=currentNode.next;}returncurrentNode;}// ✔ Returns the last node in linked list.// ✔ Does not crash AND returns null on empty list.
Clear Linked List
clear(){this.head=null;this.length=0;}// ✔ Clears out the linked list and resets length to 0.
Removing and Returning First Node
shift(){if(!this.head){return;}constoldHead=this.head;this.head=this.head.next;this.length--;returnoldHead;}// ✔ Removes AND returns first node, updates length for linked list w/ one node.// ✔ Removes the first node and returns it, decreases length of list.// ✔ Does not crash AND returns null on empty list. Does not decrease length.
Removing and Returning Last Node
pop(){if(!this.head){return;}if(this.length===1){returnthis.shift();}constlast=this.getLast();letcurrent=this.head;while(current.next!==last){current=current.next;}current.next=null;this.length--;returnlast;}// ✔ Removes AND returns last node, decreases length.// ✔ Removes AND returns last node, decreases length on linked-list w/ one node.// ✔ Returns null on empty list AND does not decrease length.
Adding a new Node to End
push(data){if(!this.head){returnthis.unshift(data);}constlast=this.getLast();last.next=newNode(data,null);this.length++;}// ✔ Adds to the end of the list and increases length.// ✔ Adds to end of empty list and increases length without crashing.
Return Node at given Index
get(index){if(index>=this.length||index<0){returnnull;}letcounter=0;letcurrent=this.head;while(counter<index){current=current.next;counter++;}returncurrent;}// ✔ Returns null on negative or out of bounds index.// ✔ Returns the node at given index.
Update Node at given Index
set(index,data){if(!this.get(index)){returnfalse;}constnode=this.get(index);node.data=data;returntrue;}// ✔ Returns falsy value on out of bounds or negative index.// ✔ Updates node and returns true.
Remove Node at given Index
remove(index){if(!this.get(index)){return;}if(index===0){returnthis.shift();}constnodeToRemove=this.get(index);constprevNode=this.get(index-1);prevNode.next=prevNode.next.next;this.length--;returnnodeToRemove;}// ✔ Returns falsy value on out of bounds OR negative index.// ✔ Removes and returns node at given index. Decreases length.// ✔ Removes node at index 0, decreases length, and returns removed node.
Insert a new Node at given Index
insert(index,data){if(index>=this.length||index<0){returnfalse;}if(index===0){this.unshift(data);returntrue;}constprevNode=this.get(index-1);constnextNode=this.get(index);prevNode.next=newNode(data,nextNode);this.length++;returntrue;}// ✔ Returns false on index greater than length or negative index.// ✔ Inserts new node at given index, increases length, and returns true.// ✔ Inserts node at 0 index correctly, increases length, returns true.
While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.
Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.
Top comments (0)