Sosovi Pio - 1 year ago 98
Java Question

convert binary to decimal using single linked list and recursive method in java

How we can convert binary to decimal using single linked list and recursive method in java? :-s

Ex:

Input: 1->0->0->NULL

Output: 4

I can think of two ways to solve it:

1- If length of list is known:

``````// To find length of list
int count = 0;
count++;
}
return count;
}

public static int convertWhenLengthIsKnown(Node head, int len) {
int sum = 0;
}
return sum + head.data * (int)Math.pow(2,len);
}

// Call this function as below:

``````
1. If we don't want to calculate length, then we can have a sum variable which is globally accessible,

``````private static int sum = 0;

public static int convert(Node head,int i) {

}
return i+1;
}

// Call this function as below:

``````

Below is the Node class:

``````class Node {
int data;
Node next;

Node(int data) {
this.data = data;
}
}
``````

Hope It helps you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download