Skip to content

Commit 89f8d9d

Browse files
solves partition list
1 parent 9fd0ab5 commit 89f8d9d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
| 81 | [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii) | [![Java](assets/java.png)](src/SearchInRotatedSortedArrayII.java) | |
7878
| 82 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedListII.java) | |
7979
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
80+
| 86 | [Partition List](https://leetcode.com/problems/partition-list) | [![Java](assets/java.png)](src/PartitionList.java) | |
8081
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
8182
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
8283
| 100 | [Same Tree](https://leetcode.com/problems/same-tree) | [![Java](assets/java.png)](src/SameTree.java) [![Python](assets/python.png)](python/same_tree.py) | |

src/PartitionList.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/partition-list
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class PartitionList {
6+
public static class ListNode {
7+
int val;
8+
ListNode next;
9+
ListNode() {}
10+
}
11+
12+
public ListNode partition(ListNode head, int x) {
13+
ListNode beforeHead = new ListNode(), beforeTemp = beforeHead;
14+
ListNode afterHead = new ListNode(), afterTemp = afterHead;
15+
while (head != null) {
16+
if (head.val < x) {
17+
beforeTemp.next = head;
18+
beforeTemp = beforeTemp.next;
19+
} else {
20+
afterTemp.next = head;
21+
afterTemp = afterTemp.next;
22+
}
23+
head = head.next;
24+
}
25+
afterTemp.next = null;
26+
beforeTemp.next = afterHead.next;
27+
return beforeHead.next;
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy