From 7af1e6437aa53b8bcfdbace9c0bab44f76d6860e Mon Sep 17 00:00:00 2001 From: VishwajeetK Date: Wed, 4 Dec 2024 11:21:48 +0530 Subject: [PATCH] refactor: Wrong logic, simplified way --- .../leet_code/easy/linkedlist/ConvertBinary.java | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/linkedlist/ConvertBinary.java b/JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/linkedlist/ConvertBinary.java index 691b701..5dc3516 100644 --- a/JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/linkedlist/ConvertBinary.java +++ b/JavaDsaWithTest/src/main/java/org/practice/dsa/leet_code/easy/linkedlist/ConvertBinary.java @@ -9,7 +9,6 @@ public static void main(String[] args) { binary.display(); System.out.println(binary.getDecimalValue()); - System.out.println(binary.binaryToDecimal(101)); } @@ -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; }