Linked List Flashcards

1
Q

How do you iterate over a linked list?

A

While (top!= null){ top = top.next }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Find value in linked list?

A

Function findVal(num){ while (top!=null){ if(top.value === num){ return top } } Return false }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a linked list?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you add a node/cell to beginning of a linked list?

A

// create a new node // set node.next = top top = new node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add a node/cell to end of linked list if there is a tail pointer?

A

// create a new node // set new node.next to null top.next = new node (note, only works if there is a tail pointer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you add a node/cell to end of linked list if there is not a tail pointer?

A

while(top.next != null){ top = top.next; } // create a new node // set new node.next to null; top.next = new node;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do insert cell/node after a certain node value?

A

while(top.next != null){ if(top.next === val){ var temp = top.next; top.next = newNode; newNode.next = temp; } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you insert an item into a sorted list?

A

while(top.next.value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you copy a linked list?

A

var newNode = new Node(); while(top.next !== null){ newNode.val = top.val; newNode.next = top.next; top = top.next; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you determine if linked list is circular via marking cells?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you determine if linked list is circular via hash table?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you determine if linked list is circular via tortoise and hare?

A

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; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write function to remove duplicate from an unsorted linked list?

A
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;
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Impelement an algorithm to find the kth to last element of a singly linked list?

A
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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Implement an algorithm to delete a node in the middle of singly linked list, given only access to that node?

A
function deleteMiddleNode(node){
 if(node.next === null || node === null){
 return false;
 }
 node.data = node.next.data;
 node.next = node.next.next;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Implement a funtion to check if a linked list is a palindrome.

A
function isPalindrome(node){
 var reversed = [];
 if(node === null){
 return null;
 }
 while(node !== null){
 reversed.push(node);
 node = node.next;
 }
 while(node !== null){
 if(node !== reversed.pop()){
 return false;
 }
 }
 return true;
}
17
Q

Given two singly linked lists, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on referenece, not value. That is, if the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, then they are intersecting.

A
  • get length and tail of each linked list
function findIntersection(list1, list2){
 if(list1 === null || list2 === null){
 return null;
 }
 var result1 = getTailAndSize(list1)
 var result2 = getTailAndSize(list2)

if(result1.tail !== result2.tail){
return null;
}

var difference = Math.abs(result1.size - result2.size)
 var shorter = result1.size \< result2.size ? result1 : result2;
 var longer = result1.size \< result2.size ? result2 : result1;
for(var i = 0; i \< difference; i++){
 longer = longer.next;
 }

while(shorter !== longer){
shorter = shorter.next
longer = longer.next
}
return shorter;
}

function getTailAndSize(list){
 if(list === null){
 return null;
 }
 var list.size = 0;
 while(list !== null){
 list = list.next;
 list.size++;
 }
 list.tail = list;
}
18
Q

Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop?

A
  1. create two pointers, fastpointer and slowpointer
  2. move fast pointer at a rate of 2 steps and slowpointer at a rate of 1 step
  3. when they collide, move slow pointer to LinkedListHead. Keep fastpointer where it is
  4. move slowpointer and fastpointer at a rate of one step. return the new collision point
function beginningOfLoop(head){
 var slow = head;
 var fast = head;
 // find meeting point. this will be loop size - k steps into the linked head
 while(fast !== null && fast.next !== null){
 slow = slow.next
 fast = fast.next.next
 if(slow === fast){
 break;
 }
 }
 // error check -- no meeting point and therefor no loop
 if(fast === null || fast.next === null){
 return null;
 }
// move slow to head. keep fast at meeting point. each are k steps from the loop start. if they
 // move at the same pace, they must meet at loop start
 slow = head;
 while(slow !== fast){
 slow = slow.next
 fast = fast.next
 }
 return fast;
}