diff options
Diffstat (limited to 'convert_bin_ll_int.c')
-rw-r--r-- | convert_bin_ll_int.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/convert_bin_ll_int.c b/convert_bin_ll_int.c new file mode 100644 index 0000000..308b204 --- /dev/null +++ b/convert_bin_ll_int.c @@ -0,0 +1,30 @@ +// 1290. Convert Binary Number in a Linked List to Integer +#include "leetcode.h" + +/* + * 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. + */ + +int getDecimalValue(struct ListNode *head) { + int ans = 0; + while (head) { + ans <<= 1; + ans += head->val; + head = head->next; + } + return ans; +} + +int main() { + struct ListNode *h1 = listnode_create(1); + h1->next = listnode_create(0); + h1->next->next = listnode_create(1); + struct ListNode *h2 = listnode_create(0); + printf("%d\n", getDecimalValue(h1)); // expect: 5 + printf("%d\n", getDecimalValue(h2)); // expect: 0 + listnode_free(h1); + listnode_free(h2); +} |