Skip to content

Commit db15f3c

Browse files
More solutions
1 parent 85c902e commit db15f3c

8 files changed

Lines changed: 194 additions & 0 deletions
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
public boolean hasCycle(ListNode head) {
14+
if (head == null || head.next == null) {
15+
return false;
16+
}
17+
ListNode first = head;
18+
ListNode second = first.next;
19+
while (second != null && first != null) {
20+
if (first == second) {
21+
return true;
22+
}
23+
second = second.next != null ? second.next.next : null;
24+
first = first.next;
25+
}
26+
return false;
27+
}
28+
}
29+
30+
31+
32+
33+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
if (headA == headB)
15+
return headA;
16+
if (headA == null || headB == null)
17+
return null;
18+
19+
ListNode first = headA;
20+
ListNode second = headB;
21+
while (first != second) {
22+
first = first != null ? first.next : headB;
23+
second = second != null ? second.next : headA;
24+
}
25+
return first;
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
// you need treat n as an unsigned value
3+
public int reverseBits(int n) {
4+
int result = 0;
5+
for (int i = 0 ; i < 32; i++) {
6+
result = result << 1;
7+
result = result | (n & 1);
8+
n = n >>> 1;
9+
}
10+
return result;
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
int count = 0;
4+
while (n > 0) {
5+
if ((n & 1) == 1) {
6+
count++;
7+
}
8+
n >>= 1;
9+
}
10+
return count;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public boolean isHappy(int n) {
5+
Set<Integer> set = new HashSet<>();
6+
while (n != 1) {
7+
if (set.contains(n)) {
8+
return false;
9+
}
10+
set.add(n);
11+
n = sumOfSquareOfDigits(n);
12+
}
13+
return true;
14+
}
15+
16+
private int sumOfSquareOfDigits(int num) {
17+
int result = 0;
18+
while (num > 0) {
19+
int rem = num % 10;
20+
result += rem * rem;
21+
num /= 10;
22+
}
23+
return result;
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public 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+
* }
10+
*/
11+
class Solution {
12+
public ListNode removeElements(ListNode head, int val) {
13+
ListNode dummy = new ListNode();
14+
dummy.next = head;
15+
16+
ListNode temp = dummy;
17+
while (temp.next != null) {
18+
if (temp.next.val == val) {
19+
temp.next = temp.next.next;
20+
} else {
21+
temp = temp.next;
22+
}
23+
}
24+
return dummy.next;
25+
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public 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+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
ListNode prev = null;
14+
ListNode current = head;
15+
16+
while (current != null) {
17+
ListNode temp = current.next;
18+
current.next = prev;
19+
prev = current;
20+
current = temp;
21+
}
22+
23+
return prev;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public int countNodes(TreeNode root) {
18+
if (root == null) return 0;
19+
return 1 + countNodes(root.left)
20+
+ countNodes(root.right);
21+
}
22+
}

0 commit comments

Comments
 (0)