Anda di halaman 1dari 2

#include <iostream>

using namespace std;

// Definisikan struktur node

struct ListNode {

int val;

ListNode* next;

ListNode(int x) : val(x), next(NULL) {}

};

int binaryToDecimal(ListNode* head) {

int decimal = 0;

ListNode* curr = head;

// Iterasi dari bit terkecil ke bit terbesar

while (curr != NULL) {

decimal = decimal * 2 + curr->val;

curr = curr->next;

return decimal;

int main() {

// Contoh penggunaan

ListNode* head = new ListNode(1);

head->next = new ListNode(0);

head->next->next = new ListNode(1);


cout << binaryToDecimal(head) << endl; // Output: 5

return 0;

Anda mungkin juga menyukai