Motorola Interview Question
Android EngineersCountry: Brazil
Interview Type: Phone Interview
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
var insertIntoSortedLL = function(node, value){
var current = node
//added to first point in LL
if (value <= current.value){
var newNode = new Node(value)
newNode.next = current;
return newNode;
}
//added into LL
while (current.next && current.next.value < value) current = current.next;
var newNode = new Node(value)
var placeholderOfNext = current.next;
current.next = newNode;
current.next.next = placeholderOfNext;
return node;
}
var linkedList = new Node(2);
linkedList.next = new Node(7);
insertIntoSortedLL(linkedList, 8);
class Node{
constructor(value){
this.value = value;
this.next = null;
}
}
var insertIntoSortedLL = function(node, value){
var current = node;
//added to first point in LL
if (value <= current.value){
var newNode = new Node(value)
newNode.next = current;
return newNode;
}
//added into LL
while (current.next && current.next.value < value) current = current.next;
var newNode = new Node(value)
var placeholderOfNext = current.next;
current.next = newNode;
current.next.next = placeholderOfNext;
return node;
}
var linkedList = new Node(2);
linkedList.next = new Node(7);
console.log(insertIntoSortedLL(linkedList, 8));
Heap sort
- Anonymous November 08, 2016