2 Convert Binary Number in a Linked List to Integer --> Leetcode November 2020 Challenge

2 Convert Binary Number in a Linked List to Integer --> Leetcode November 2020 Challenge

This is the second problem posted in Leetcode November 2020 Challenge.

Problem Statement

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.

Example 1:

image.png

Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10

Constraints:

The Linked List is not empty. Number of nodes will not exceed 30. Each node's value is either 0 or 1.

Solution The idea is to traverse the linked list and store all values in array and then use mathematical definition to convert binary number into decimal digit

101 in base 2 = 1 X 2 to the power 2 + 0 X 2 to the power 1 + 1 X 2 to the power 0

#include<bits/stdc++.h>
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        vector<int> binaryDigits;
        ListNode* iterator = head;
        while(iterator != nullptr){
            binaryDigits.push_back(iterator->val);
            iterator = iterator->next;
        }
        int size = binaryDigits.size();
        int sum = 0;
        for(int i = 0; i < size; i++){
            sum = sum + pow(2,size-1-i)*binaryDigits[i];
        }
        return sum;
    }
};

However this process takes memory O(N) and can be reduced to O(1) using the following approach

num = num * 2 + x; // where x is the next val
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int num = head->val;
        while(head->next != nullptr){
            num = num * 2 + head->next->val;
            head = head->next;
        }
        return num;
    }
};

This can be further increased performance using the bit arithmatics. << --> is called bitwise shift left operator | --> is called bitwise OR operator

1 shift to the left ( num << 1) is equivalent to (num * 2 ^ 1) OR operator adds the value bit wise

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int num = head->val;
        while(head->next != nullptr){
            num = (num << 1) | head->next->val ;
            head = head->next;
        }
        return num;
    }
};

This is a quick problem that gives basic idea on using bitwise operator and shows how we can traverse through the linked list using next pointer. The final summary is to traverse the linked list and store all values in a string or array and convert the values obtained to decimal value. Also to increase the performance and space complexity we can solve the problem in O(1) memory using bits operation, shift left operation ( << ) and or operation ( | ) to get the decimal value in one operation.

Thank you all.