How to convert a binary linked list to decimal in Java

Day 53/100 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. The most significant bit is at the head of the linked list. Input: head = [1,0,1] Output: 5 Solution : https://lnkd.in/g3vuQeUx public int getDecimalValue(ListNode head) { ListNode temp=head; String s=""; while(temp!=null){ s+=temp.val; temp=temp.next; } int ans=Integer.parseInt(s,2); return ans; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF

To view or add a comment, sign in

Explore content categories