Created
March 2, 2017 18:46
-
-
Save vitkarpov/5bb8d56cf86bd20ad6c10d96ca5d7158 to your computer and use it in GitHub Desktop.
"Cracking the coding interview". Linked list, 2.1.2
This file contains hidden or 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
/** | |
* Удаляет дубликаты из связаного списка. | |
* Не использует дополнительную память. | |
* Функция принимает на вход указатель на начало списка. | |
* | |
* @param {Node} head | |
* @returns {Node} | |
*/ | |
function removeNode(head) { | |
// Предполагается, что head объект вида { data: <*>, next: <Node> } | |
let current = head; | |
while (current !== null) { | |
let runner = current; | |
while (runner.next !== null) { | |
if (runner.next.data === current.data) { | |
runner.next = runner.next.next; | |
} else { | |
runner = runner.next; | |
} | |
} | |
current = current.next; | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment