-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1290.GetDecimalValue.cs
71 lines (63 loc) · 2.06 KB
/
1290.GetDecimalValue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 1290. Convert Binary Number in a Linked List to Integer
// Given head which is a reference node to a singly-linked list.
// The value of each node in the linked list is either 0 or 1.
// The linked list holds the binary representation of a number.
// Return the decimal value of the number in the linked list.
// The most significant bit is at the head of the linked list.
// Example 1:
// Input: head = [1,0,1]
// Output: 5
// Explanation: (101) in base 2 = (5) in base 10
// Example 2:
// Input: head = [0]
// Output: 0
// Constraints:
// The Linked List is not empty.
// Number of nodes will not exceed 30.
// Each node's value is either 0 or 1.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
//Intution:
public int GetDecimalValue(ListNode head) {
int result = 0;
int pow = -1;
ListNode curr = head;
//First we need to find the length of the linked list
while(curr != null){
pow++;
curr = curr.next;
}
curr = head;
while(curr != null){
//We can calculate the decimal value of the linked list by multiplying the value of
//the current node by 2^pow where pow is the length of the linked list - 1
result += curr.val * (int)Math.Pow(2,pow);
pow--;
curr = curr.next;
}
return result;
}
}
//Another solution using StringBuilder and Convert.ToInt32
public class Solution {
public int GetDecimalValue(ListNode head) {
StringBuilder sb = new();
while(head != null){
//We can use StringBuilder to append the value of each node in the linked list
sb.Append(head.val);
head= head.next;
}
//We can then use Convert.ToInt32 to convert the binary string to an integer
return Convert.ToInt32(sb.ToString(), 2);
}
}