Skip to content

Commit ef96f89

Browse files
🔢 Day 1
1 parent e5af663 commit ef96f89

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[Challenge Link](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/)
66

77
## Week 1 🚧
8-
Coming soon...
8+
1. [Convert Binary Number in a Linked List to Integer](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3516/) ➡️ [CPP Solution](Week1/getDecimalValue.cpp)
99

1010
## Week 2 🚧
1111
Coming soon...

Week1/getDecimalValue.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
int getDecimalValue(ListNode* head) {
14+
if(head == NULL) return 0;
15+
int len = 0;
16+
ListNode* curr = head;
17+
18+
while(curr != NULL) {
19+
curr = curr->next;
20+
len++;
21+
}
22+
23+
curr = head;
24+
int num = 0;
25+
26+
while(curr != NULL) {
27+
num += curr->val * pow(2, len - 1);
28+
curr = curr->next;
29+
len--;
30+
}
31+
32+
return num;
33+
}
34+
};

0 commit comments

Comments
 (0)