File tree 2 files changed +77
-28
lines changed
2 files changed +77
-28
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ;
7
7
for (int num : nums ) {
8
- if (candidate1 != null && candidate1 == num ) {
9
- count1 ++;
8
+ if (candidateOne != null && candidateOne == num ) {
9
+ countOne ++;
10
10
}
11
- else if (candidate2 != null && candidate2 == num ) {
12
- count2 ++;
11
+ else if (candidateTwo != null && candidateTwo == num ) {
12
+ countTwo ++;
13
13
}
14
- else if (count1 == 0 ) {
15
- candidate1 = num ;
16
- count1 = 1 ;
14
+ else if (countOne == 0 ) {
15
+ candidateOne = num ;
16
+ countOne = 1 ;
17
17
}
18
- else if (count2 == 0 ) {
19
- candidate2 = num ;
20
- count2 = 1 ;
18
+ else if (countTwo == 0 ) {
19
+ candidateTwo = num ;
20
+ countTwo = 1 ;
21
21
}
22
22
else {
23
- count1 --;
24
- count2 --;
23
+ countOne --;
24
+ countTwo --;
25
25
}
26
26
}
27
- List <Integer > ans = new ArrayList <>();
28
- count1 = 0 ;
29
- count2 = 0 ;
27
+ List <Integer > result = new ArrayList <>();
28
+ countOne = 0 ;
29
+ countTwo = 0 ;
30
30
for (int num : nums ) {
31
- if (candidate1 != null && candidate1 == num ) {
32
- count1 ++;
31
+ if (candidateOne != null && candidateOne == num ) {
32
+ countOne ++;
33
33
}
34
- else if (candidate2 != null && candidate2 == num ) {
35
- count2 ++;
34
+ else if (candidateTwo != null && candidateTwo == num ) {
35
+ countTwo ++;
36
36
}
37
37
}
38
- if (count1 > nums .length / 3 ) {
39
- ans .add (candidate1 );
38
+ if (countOne > nums .length / 3 ) {
39
+ result .add (candidateOne );
40
40
}
41
- if (count2 > nums .length / 3 ) {
42
- ans .add (candidate2 );
41
+ if (countTwo > nums .length / 3 ) {
42
+ result .add (candidateTwo );
43
43
}
44
- return ans ;
44
+ return result ;
45
45
}
46
46
}
You can’t perform that action at this time.
0 commit comments