|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 86. Partition List *- |
| 4 | +
|
| 5 | +
|
| 6 | +Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. |
| 7 | +
|
| 8 | +You should preserve the original relative order of the nodes in each of the two partitions. |
| 9 | +
|
| 10 | + |
| 11 | +
|
| 12 | +Example 1: |
| 13 | +
|
| 14 | +
|
| 15 | +Input: head = [1,4,3,2,5,2], x = 3 |
| 16 | +Output: [1,2,2,4,3,5] |
| 17 | +Example 2: |
| 18 | +
|
| 19 | +Input: head = [2,1], x = 2 |
| 20 | +Output: [1,2] |
| 21 | + |
| 22 | +
|
| 23 | +Constraints: |
| 24 | +
|
| 25 | +The number of nodes in the list is in the range [0, 200]. |
| 26 | +-100 <= Node.val <= 100 |
| 27 | +-200 <= x <= 200 |
| 28 | +
|
| 29 | +
|
| 30 | +
|
| 31 | +*/ |
| 32 | + |
| 33 | +// Definition for singly-linked list. |
| 34 | +class ListNode { |
| 35 | + int val; |
| 36 | + ListNode? next; |
| 37 | + ListNode([this.val = 0, this.next]); |
| 38 | +} |
| 39 | + |
| 40 | +class Solution { |
| 41 | + ListNode? partition(ListNode? head, int x) { |
| 42 | + ListNode left = ListNode(0); |
| 43 | + ListNode right = ListNode(0); |
| 44 | + ListNode temp1 = left; |
| 45 | + ListNode temp2 = right; |
| 46 | + |
| 47 | + while (head != null) { |
| 48 | + if (head.val < x) { |
| 49 | + left.next = head; |
| 50 | + left = left.next!; |
| 51 | + } else { |
| 52 | + right.next = head; |
| 53 | + right = right.next!; |
| 54 | + } |
| 55 | + head = head.next; |
| 56 | + } |
| 57 | + |
| 58 | + left.next = temp2.next; |
| 59 | + right.next = null; |
| 60 | + |
| 61 | + return temp1.next; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class MERGESort { |
| 66 | + ListNode? merge(ListNode? firstList, ListNode? secondList, int x) { |
| 67 | + if (firstList == null) { |
| 68 | + return secondList; |
| 69 | + } |
| 70 | + if (secondList == null) { |
| 71 | + return firstList; |
| 72 | + } |
| 73 | + |
| 74 | + ListNode newHead = ListNode(-9); |
| 75 | + ListNode temp = newHead; |
| 76 | + |
| 77 | + while (firstList != null && secondList != null) { |
| 78 | + if (firstList.val < x) { |
| 79 | + temp.next = firstList; |
| 80 | + firstList = firstList.next; |
| 81 | + } else if (secondList.val < x) { |
| 82 | + temp.next = secondList; |
| 83 | + secondList = secondList.next; |
| 84 | + } else { |
| 85 | + break; |
| 86 | + } |
| 87 | + temp = temp.next!; |
| 88 | + } |
| 89 | + |
| 90 | + if (firstList != null) { |
| 91 | + temp.next = firstList; |
| 92 | + while (temp.next != null) { |
| 93 | + temp = temp.next!; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (secondList != null) { |
| 98 | + temp.next = secondList; |
| 99 | + } |
| 100 | + |
| 101 | + return newHead.next; |
| 102 | + } |
| 103 | + |
| 104 | + ListNode? partition(ListNode? head, int x) { |
| 105 | + if (head == null || head.next == null) { |
| 106 | + return head; |
| 107 | + } |
| 108 | + |
| 109 | + ListNode? temp = head; |
| 110 | + ListNode? slow = head.next; |
| 111 | + ListNode? fast = slow?.next; |
| 112 | + |
| 113 | + while (fast != null && fast.next != null) { |
| 114 | + temp = slow; |
| 115 | + slow = slow?.next; |
| 116 | + fast = fast.next?.next; |
| 117 | + } |
| 118 | + |
| 119 | + temp?.next = null; |
| 120 | + |
| 121 | + return merge(partition(head, x), partition(slow, x), x); |
| 122 | + } |
| 123 | +} |
0 commit comments