Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2e8276

Browse files
Add Solution.java to problems 1290
1 parent 886594f commit d2e8276

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public int getDecimalValue(ListNode head) {
11+
int sum = 0;
12+
StringBuilder sb = new StringBuilder("0");
13+
while (head != null) {
14+
sum += head.val;
15+
if (sum != 0) {
16+
sb.append(head.val);
17+
}
18+
head = head.next;
19+
}
20+
return Integer.valueOf(sb.toString(), 2);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public int getDecimalValue(ListNode head) {
11+
int count = 0;
12+
if (head != null) {
13+
while(head != null){
14+
count = (count << 1) + head.val;
15+
head = head.next;
16+
}
17+
}
18+
return count;
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /