Skip to content

Commit 1b90428

Browse files
committed
Added one solution & modified one solution
1 parent 1b45077 commit 1b90428

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node next;
6+
7+
public Node() {}
8+
9+
public Node(int _val) {
10+
val = _val;
11+
}
12+
13+
public Node(int _val, Node _next) {
14+
val = _val;
15+
next = _next;
16+
}
17+
};
18+
*/
19+
20+
class Solution {
21+
public Node insert(Node head, int insertVal) {
22+
if (head == null) {
23+
Node newNode = new Node(insertVal);
24+
newNode.next = newNode;
25+
return newNode;
26+
}
27+
Node prev = head;
28+
Node curr = head.next;
29+
boolean insert = false;
30+
do {
31+
if (prev.val <= insertVal && insertVal <= curr.val) {
32+
insert = true;
33+
}
34+
else if (prev.val > curr.val) {
35+
if (insertVal >= prev.val || insertVal <= curr.val) {
36+
insert = true;
37+
}
38+
}
39+
if (insert) {
40+
prev.next = new Node(insertVal, curr);
41+
return head;
42+
}
43+
prev = curr;
44+
curr = curr.next;
45+
} while (prev != head);
46+
prev.next = new Node(insertVal, curr);
47+
return head;
48+
}
49+
}

Medium/Majority Element II.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
class Solution {
22
public List<Integer> majorityElement(int[] nums) {
3-
int count1 = 0;
4-
int count2 = 0;
5-
Integer candidate1 = null;
6-
Integer candidate2 = null;
3+
int countOne = 0;
4+
int countTwo = 0;
5+
Integer candidateOne = null;
6+
Integer candidateTwo = null;
77
for (int num : nums) {
8-
if (candidate1 != null && candidate1 == num) {
9-
count1++;
8+
if (candidateOne != null && candidateOne == num) {
9+
countOne++;
1010
}
11-
else if (candidate2 != null && candidate2 == num) {
12-
count2++;
11+
else if (candidateTwo != null && candidateTwo == num) {
12+
countTwo++;
1313
}
14-
else if (count1 == 0) {
15-
candidate1 = num;
16-
count1 = 1;
14+
else if (countOne == 0) {
15+
candidateOne = num;
16+
countOne = 1;
1717
}
18-
else if (count2 == 0) {
19-
candidate2 = num;
20-
count2 = 1;
18+
else if (countTwo == 0) {
19+
candidateTwo = num;
20+
countTwo = 1;
2121
}
2222
else {
23-
count1--;
24-
count2--;
23+
countOne--;
24+
countTwo--;
2525
}
2626
}
27-
List<Integer> ans = new ArrayList<>();
28-
count1 = 0;
29-
count2 = 0;
27+
List<Integer> result = new ArrayList<>();
28+
countOne = 0;
29+
countTwo = 0;
3030
for (int num : nums) {
31-
if (candidate1 != null && candidate1 == num) {
32-
count1++;
31+
if (candidateOne != null && candidateOne == num) {
32+
countOne++;
3333
}
34-
else if (candidate2 != null && candidate2 == num) {
35-
count2++;
34+
else if (candidateTwo != null && candidateTwo == num) {
35+
countTwo++;
3636
}
3737
}
38-
if (count1 > nums.length / 3) {
39-
ans.add(candidate1);
38+
if (countOne > nums.length / 3) {
39+
result.add(candidateOne);
4040
}
41-
if (count2 > nums.length / 3) {
42-
ans.add(candidate2);
41+
if (countTwo > nums.length / 3) {
42+
result.add(candidateTwo);
4343
}
44-
return ans;
44+
return result;
4545
}
4646
}

0 commit comments

Comments
 (0)