Linked Lists Flashcards

1
Q

Prompt:
Given a linked list and a target k, return a linked list containing every kth element in the list.

Example:
input = 1 -> 3 -> 6 -> 2 -> 8 -> 9, k = 3 returns a new list => 6 -> 9

A

function everyKthNode(node, target) {

if (node == null) return null;

  let current1 = node;
  let newList = new LLNode(0);
  let current2 = newList;
  let i = 1;
  while (current1) {
    if (i ===  target) {
      current2.next = new LLNode(current1.value);
      current2 = current2.next;
      i = 0;
    }
current1 = current1.next;
i += 1;   }

return newList.next;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Prompt
Given a linked list of numbers, return the first missing value. The list doesn't have to start at 1. From where ever it starts expect each successive number to be one greater.
Function Signature
function findMissing(head)
Expected Runtime
O(n)
Examples
1 => 2
1, 2 => 3
1, 3 => 2
-1 => 0
-3, -1 => -2
45, 47 => 46
A

Note - my solution:

function findMissing(head) {
 let curr = head;

if(head === null){
return 1;
}

while(curr){
if(curr.next === null){
return curr.value + 1;
}

if(curr.next.value !== curr.value + 1){
return curr.value + 1;
}

curr = curr.next;
}

}

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