Method
partition
(LinkedListNode node, int x)
Source from the content-addressed store, hash-verified
| 5 | public class QuestionC { |
| 6 | |
| 7 | public static LinkedListNode partition(LinkedListNode node, int x) { |
| 8 | LinkedListNode head = node; |
| 9 | LinkedListNode tail = node; |
| 10 | |
| 11 | /* Partition list */ |
| 12 | while (node != null) { |
| 13 | LinkedListNode next = node.next; |
| 14 | if (node.data < x) { |
| 15 | /* Insert node at head. */ |
| 16 | node.next = head; |
| 17 | head = node; |
| 18 | } else { |
| 19 | /* Insert node at tail. */ |
| 20 | tail.next = node; |
| 21 | tail = node; |
| 22 | } |
| 23 | node = next; |
| 24 | } |
| 25 | tail.next = null; |
| 26 | |
| 27 | return head; |
| 28 | } |
| 29 | |
| 30 | public static void main(String[] args) { |
| 31 | int length = 20; |
Tested by
no test coverage detected