/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
cracking-the-coding-interview/chapter-two-linked-lists/Partition.java
28 строк
648 B
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
//Write code to partition a linked list around a value x, such that //all nodes less than x come before all nodes greater than or equal to x. public class Partition { LinkedListNode partition(LinkedListNode node, int x) { LinkedListNode head = node; LinkedListNode tail = node; while(node != null) { LinkedListNode next = node.next; if(node.data < x) { /* insert node at head */ node.next = head; head = node; } else { /* insert node at tail */ tail.next = node; tail = node; } node = next; } tail.next = null; //the head has changed, so we need to return it to the user return head; } }