Skip to content

Commit d6baed5

Browse files
committed
add linked-list-cycle, sum-of-two-integers
1 parent 44e012c commit d6baed5

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

linked-list-cycle/sangyyypark.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
public class Solution {
13+
public boolean hasCycle(ListNode head) {
14+
Set<ListNode> set = new HashSet();
15+
ListNode current = head;
16+
set.add(current);
17+
while(current != null) {
18+
if(set.contains(current.next)) {
19+
return true;
20+
}
21+
current = current.next;
22+
set.add(current);
23+
}
24+
return false;
25+
26+
}
27+
}
28+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int getSum(int a, int b) {
3+
while(b != 0) {
4+
int sum = a ^ b;
5+
int carry = (a & b) << 1;
6+
a = sum;
7+
b = carry;
8+
}
9+
return a;
10+
}
11+
}
12+

0 commit comments

Comments
 (0)