Created
February 16, 2017 17:34
-
-
Save mikeliao97/719cb891f7aebf4df8a3c8fe3a12f4df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LinkedList = function(value) { | |
this.value = value; | |
this.next = null; | |
} | |
//REturns a unique linked list based on the argument | |
function uniqueLinkedList(link_list) { | |
var uniqueValues = []; | |
//Iterate through the passed in linked_list | |
while (link_list) { | |
if (uniqueValues.indexOf(link_list.value) === -1) { | |
uniqueValues.push(link_list.value); | |
} | |
link_list = link_list.next; | |
} | |
//uniqueVAlues = [3, 1, 2] | |
//REcrate a new linkedList based on the values; | |
var newLinkedList = new LinkedList(); | |
var pointer = newLinkedList; | |
while(uniqueValues) { | |
var toAdd = uniqueValues.shift(); | |
if (typeof pointer === undefined) { //create new one | |
} else { | |
pointer = pointer.next; | |
} | |
return newLinkedList; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment