Skip to content

Commit

Permalink
refactor: Wrong logic, simplified way
Browse files Browse the repository at this point in the history
  • Loading branch information
VishwajeetVT committed Dec 4, 2024
1 parent d7cadb9 commit 7af1e64
Showing 1 changed file with 1 addition and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public static void main(String[] args) {
binary.display();

System.out.println(binary.getDecimalValue());
System.out.println(binary.binaryToDecimal(101));

}

Expand All @@ -18,22 +17,9 @@ public int getDecimalValue() {
ListNode node = head;
int result = 0;
while (node != null) {
result = result * 10 + node.val;
result = result * 2 + node.val;
node = node.next;
}

return binaryToDecimal(result);
}

private int binaryToDecimal(int binaryNumber) {
int power = 0;
int result = 0;
while (binaryNumber > 0) {
int lastDigit = binaryNumber % 10;
result += (int) (lastDigit * Math.pow(2, power));
binaryNumber = binaryNumber/10;
power++;
}
return result;
}

Expand Down

0 comments on commit 7af1e64

Please sign in to comment.