Skip to content

Commit e880e36

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 759a935 commit e880e36

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

Easy/Middle of the linked list.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,13 @@
77
* }
88
*/
99
class Solution {
10-
public ListNode middleNode(ListNode head) {
11-
int l = getLengthOfLinkedList(head);
12-
int mid = l/2;
13-
14-
while (mid > 0) {
15-
head = head.next;
16-
mid--;
17-
}
18-
19-
return head;
20-
}
21-
22-
private int getLengthOfLinkedList(ListNode head) {
23-
int c = 0;
24-
while(head != null) {
25-
head = head.next;
26-
c++;
27-
}
28-
29-
return c;
10+
public ListNode middleNode(ListNode head) {
11+
ListNode slow = head;
12+
ListNode fast = head;
13+
while (fast != null && fast.next != null) {
14+
slow = slow.next;
15+
fast = fast.next.next;
3016
}
17+
return slow;
18+
}
3119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<Integer> minSubsequence(int[] nums) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
4+
public int compare(Integer o1, Integer o2) {
5+
return o2 - o1;
6+
}
7+
});
8+
int totalSum = 0;
9+
for (int num : nums) {
10+
pq.add(num);
11+
totalSum += num;
12+
}
13+
List<Integer> list = new ArrayList<>();
14+
int currSum = 0;
15+
while (currSum <= totalSum) {
16+
int num = pq.poll();
17+
currSum += num;
18+
totalSum -= num;
19+
list.add(num);
20+
}
21+
return list;
22+
}
23+
}

0 commit comments

Comments
 (0)