Skip to content

Commit 20382be

Browse files
solves #109 in java
1 parent a2ddf9e commit 20382be

20 files changed

+56
-129
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
| 106 | [Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal) | [![Java](assets/java.png)](src/ConstructBinaryTreeFromInorderAndPostorderTraversal.java) | |
102102
| 107 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii) | [![Java](assets/java.png)](src/BinaryTreeLevelOrderTraversalII.java) [![Python](assets/python.png)](python/binary_tree_level_order_traversal_ii.py) | |
103103
| 108 | [Convert Sorted Array To Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree) | [![Java](assets/java.png)](src/ConvertSortedArrayToBinarySearchTree.java) [![Python](assets/python.png)](python/converted_sorted_array_to_binary_search_tree.py) | |
104-
| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree) | | |
104+
| 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree) | [![Java](assets/java.png)](src/ConvertSortedListToBinarySearchTree.java) | |
105105
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree) | [![Java](assets/java.png)](src/BalancedBinaryTree.java) [![Python](assets/python.png)](python/balanced_binary_tree.py) | |
106106
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree) | [![Java](assets/java.png)](src/MinimumDepthOfBinaryTree.java) [![Python](assets/python.png)](python/minimum_depth_of_binary_tree.py) | |
107107
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | [![Java](assets/java.png)](src/PathSum.java) [![Python](assets/python.png)](python/path_sum.py) | |

src/AddTwoNumbers.java

-8
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,4 @@ private int get(ListNode node) {
3333
if (node == null) return 0;
3434
return node.val;
3535
}
36-
37-
public static class ListNode {
38-
int val;
39-
ListNode next;
40-
ListNode() {}
41-
ListNode(int val) { this.val = val; }
42-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
43-
}
4436
}

src/ConvertBinaryNumberInLinkedListToInteger.java

-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,4 @@ public int getDecimalValue(ListNode head) {
88
}
99
return value;
1010
}
11-
12-
13-
private static class ListNode {
14-
int val;
15-
ListNode next;
16-
ListNode() {};
17-
ListNode(int val) { this.val = val; }
18-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
19-
}
2011
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree
2+
// T: O(n)
3+
// S: O(n)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class ConvertSortedListToBinarySearchTree {
9+
public TreeNode sortedListToBST(ListNode head) {
10+
final List<Integer> list = toList(head);
11+
return listToBST(list);
12+
}
13+
14+
private TreeNode listToBST(List<Integer> list) {
15+
return listToBST(list, 0, list.size());
16+
}
17+
18+
private TreeNode listToBST(List<Integer> list, int start, int end) {
19+
if (start == end) return null;
20+
21+
final int middle = start + (end - start) / 2;
22+
final TreeNode root = new TreeNode(list.get(middle));
23+
root.left = listToBST(list, start, middle);
24+
root.right = listToBST(list, middle + 1, end);
25+
return root;
26+
}
27+
28+
private List<Integer> toList(ListNode head) {
29+
final List<Integer> result = new ArrayList<>();
30+
while (head != null) {
31+
result.add(head.val);
32+
head = head.next;
33+
}
34+
return result;
35+
}
36+
}

src/DeleteANodeInLinkedList.java

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
public class DeleteANodeInLinkedList {
2-
private static class ListNode {
3-
int val;
4-
ListNode next;
5-
}
62

73
public static void deleteNode(ListNode node) {
84
ListNode current = node;

src/IntersectionOf2LinkedLists.java

-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
import java.util.Set;
33

44
public class IntersectionOf2LinkedLists {
5-
public static class ListNode {
6-
int val;
7-
ListNode next;
8-
ListNode(int x) {
9-
val = x;
10-
next = null;
11-
}
12-
}
135

146
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
157
Set<ListNode> aNodes = new HashSet<>();

src/LinkedListCycle.java

-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
// S: O(1)
33

44
public class LinkedListCycle {
5-
private static class ListNode {
6-
int val;
7-
ListNode next;
8-
ListNode(int x) {
9-
val = x;
10-
next = null;
11-
}
12-
}
13-
145
public boolean hasCycle(ListNode head) {
156
if (head == null) return false;
167

src/ListNode.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode() { }
6+
7+
ListNode(int val) {
8+
this.val = val;
9+
}
10+
11+
ListNode(int val, ListNode next) {
12+
this.val = val;
13+
this.next = next;
14+
}
15+
}

src/Merge2SortedLists.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://leetcode.com/problems/merge-two-sorted-lists/
1+
// https://leetcode.com/problems/merge-two-sorted-lists
22

33
public class Merge2SortedLists {
44
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
@@ -19,12 +19,4 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1919
previous.next = l1 == null ? l2 : l1;
2020
return result.next;
2121
}
22-
23-
public static class ListNode {
24-
int val;
25-
ListNode next;
26-
ListNode() {}
27-
ListNode(int val) { this.val = val; }
28-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
29-
}
3022
}

src/MiddleOfTheLinkedList.java

-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
public class MiddleOfTheLinkedList {
2-
public static class ListNode {
3-
int val;
4-
ListNode next;
5-
ListNode() {}
6-
ListNode(int val) { this.val = val; }
7-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
8-
}
9-
102
public ListNode middleNode(ListNode head) {
113
ListNode slow = head, fast = head;
124
while (fast != null && fast.next != null) {

src/PalindromeLinkedList.java

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import java.util.List;
33

44
public class PalindromeLinkedList {
5-
private static class ListNode {
6-
int val;
7-
ListNode next;
8-
}
95

106
public static boolean isPalindrome(ListNode head) {
117
List<Integer> list = listFrom(head);

src/PartitionList.java

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
// S: O(1)
44

55
public class PartitionList {
6-
public static class ListNode {
7-
int val;
8-
ListNode next;
9-
ListNode() {}
10-
}
116

127
public ListNode partition(ListNode head, int x) {
138
ListNode beforeHead = new ListNode(), beforeTemp = beforeHead;

src/RemoveDuplicatesFromSortedList.java

-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
11
public class RemoveDuplicatesFromSortedList {
2-
public static class ListNode {
3-
int val;
4-
ListNode next;
5-
6-
ListNode() {
7-
}
8-
9-
ListNode(int val) {
10-
this.val = val;
11-
}
12-
13-
ListNode(int val, ListNode next) {
14-
this.val = val;
15-
this.next = next;
16-
}
17-
}
182

193
public ListNode deleteDuplicates(ListNode head) {
204
ListNode current = head;

src/RemoveDuplicatesFromSortedListII.java

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
// S: O(1)
44

55
public class RemoveDuplicatesFromSortedListII {
6-
private static class ListNode {
7-
int val;
8-
ListNode next;
9-
}
106

117
public ListNode deleteDuplicates(ListNode head) {
128
if (head == null) return null;

src/RemoveLinkedListElements.java

-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
public class RemoveLinkedListElements {
2-
private static class ListNode {
3-
int val;
4-
ListNode next;
5-
6-
ListNode(int val, ListNode next) {
7-
this.val = val;
8-
this.next = next;
9-
}
10-
}
112

123
public static ListNode removeElements(ListNode head, int target) {
134
ListNode result = new ListNode(-1, head), current = result;

src/RemoveNthNodeFromEndOfList.java

-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
21
public class RemoveNthNodeFromEndOfList {
3-
public static class ListNode {
4-
int val;
5-
ListNode next;
6-
ListNode() {}
7-
ListNode(int val) { this.val = val; }
8-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9-
}
102

113
public ListNode removeNthFromEnd(ListNode head, int n) {
124
final int length = length(head);

src/ReverseLinkedList.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
// T: O(n)
2+
// S: O(1)
3+
14
public class ReverseLinkedList {
2-
private static class ListNode {
3-
int val;
4-
ListNode next;
5-
}
65

76
public static ListNode reverseList(ListNode head) {
87
if (head == null || head.next == null) {

src/ReverseLinkedListII.java

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
// S: O(1)
33

44
public class ReverseLinkedListII {
5-
public static class ListNode {
6-
int val;
7-
ListNode next;
8-
ListNode() {}
9-
ListNode(int val) { this.val = val; }
10-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
11-
}
125

136
public ListNode reverseBetween(ListNode head, int left, int right) {
147
if (left == right) return head;

src/RotateList.java

-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
// S: O(1)
44

55
public class RotateList {
6-
public static class ListNode {
7-
int val;
8-
ListNode next;
9-
ListNode() {}
10-
ListNode(int val) { this.val = val; }
11-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
12-
}
136

147
public ListNode rotateRight(ListNode head, int k) {
158
final int length = getLength(head);

src/SwapNodesInPairs.java

-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33
// S: O(1)
44

55
public class SwapNodesInPairs {
6-
private static class ListNode {
7-
int val;
8-
ListNode next;
9-
10-
ListNode(int val, ListNode next) {
11-
this.val = val;
12-
this.next = next;
13-
}
14-
}
156

167
public ListNode swapPairs(ListNode head) {
178
if (head == null || head.next == null) return head;

0 commit comments

Comments
 (0)