Linked List Flashcards
How do you iterate over a linked list?
While (top!= null){ top = top.next }
Find value in linked list?
Function findVal(num){ while (top!=null){ if(top.value === num){ return top } } Return false }
What is a linked list?
Group of nodes which together represent a sequence. Each node is composed of data and a reference to the next node in the sequence. Linked list has A top or head pointer Last nodes next value points to null
How do you add a node/cell to beginning of a linked list?
// create a new node // set node.next = top top = new node
How do you add a node/cell to end of linked list if there is a tail pointer?
// create a new node // set new node.next to null top.next = new node (note, only works if there is a tail pointer)
How do you add a node/cell to end of linked list if there is not a tail pointer?
while(top.next != null){ top = top.next; } // create a new node // set new node.next to null; top.next = new node;
How do insert cell/node after a certain node value?
while(top.next != null){ if(top.next === val){ var temp = top.next; top.next = newNode; newNode.next = temp; } }
How do you insert an item into a sorted list?
while(top.next.value
How do you copy a linked list?
var newNode = new Node(); while(top.next !== null){ newNode.val = top.val; newNode.next = top.next; top = top.next; }
How do you determine if linked list is circular via marking cells?
var is_circular = false; while(top.next !== null){ if(top.next.visited === true){ top.next = null; is_circular = true; } top = top.next; top.is_circular = true; }
How do you determine if linked list is circular via hash table?
var obj = {}; while(top.next !== null){ if(obj.hasOwnProperty(top.next)){ top.next = null; return true; } obj[top.next] = top.next; obj[top.visited] = true; top = top.next; return false; }
How do you determine if linked list is circular via tortoise and hare?
function hasCycle(linkedList){ var fast = linkedList; var slow = linkedList; var pause = true; while(fast.next){ if(fast === slow){ return true; } if(!pause){ slow = slow.next; } pause = !pause; } return false; }
Write function to remove duplicate from an unsorted linked list?
function removeDups(node){ var ht = {}; var previous = null; while(node !== null){ if(ht.hasOwnProperty(node.data)){ previous.next = node.next; } else{ ht[node.data] = node.data; previous = node; } node = node.next; } }
Impelement an algorithm to find the kth to last element of a singly linked list?
function findKthToLast(head, k){ var list1 = head var list2 = head for(var i = 0; i \< k; i++){ if(list1 === null){ return null; } list1 = list1.next; } while(list1 !== null){ list1 = list1.next list2 = list2.next } return list2 }
Implement an algorithm to delete a node in the middle of singly linked list, given only access to that node?
function deleteMiddleNode(node){ if(node.next === null || node === null){ return false; } node.data = node.next.data; node.next = node.next.next; }