Java Program To Find Decimal Equivalent Of Binary Linked List Last Updated : 22 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given a singly linked list of 0s and 1s find its decimal equivalent. Input: 0->0->0->1->1->0->0->1->0 Output: 50 Input: 1->0->0 Output: 4 The decimal value of an empty linked list is considered as 0. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Initialize the result as 0. Traverse the linked list and for each node, multiply the result by 2 and add the node's data to it. Java // Java Program to find decimal value // of binary linked list class GFG{ // Link list Node static class Node { boolean data; Node next; }; // Returns decimal value of binary // linked list static int decimalValue(Node head) { // Initialized result int res = 0; // Traverse linked list while (head != null) { // Multiply result by 2 and // add head's data res = (res << 1) + (head.data?1:0); // Move next head = head.next; } return res; } // Utility function to create a // new node. static Node newNode(int data) { Node temp = new Node(); temp.data = (data == 1 ? true : false); temp.next = null; return temp; } // Driver code public static void main(String args[]) { // Start with the empty list Node head = newNode(1); head.next = newNode(0); head.next.next = newNode(1); head.next.next.next = newNode(1); System.out.print("Decimal value is " + decimalValue(head)); } } // This code is contributed by Arnab Kundu Output : Decimal value is 11 Time Complexity: O(n) where n is the number of nodes in the given linked list.Auxiliary Space: O(1), no extra space is required, so it is a constant. Please refer complete article on Decimal Equivalent of Binary Linked List for more details! Comment More infoAdvertise with us Next Article Java Program To Find Decimal Equivalent Of Binary Linked List kartik Follow Improve Article Tags : Linked List Java Programs DSA Juniper Networks Practice Tags : Juniper NetworksLinked List Similar Reads Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java Program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3-> 3 min read Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g 4 min read Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3Â Input: 1->2->3->4->5-> 6 min read Java Program to Illustrate Use of Binary Literals A binary literal is a number represented in binary (0s and 1s). In Java, you can use binary literals for integral types such as byte, short, int, and long. To specify a binary literal, prefix the number with 0b or 0B. This allows you to define numbers in binary format directly in your code. When exe 4 min read Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi 4 min read Java Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 4 min read Java Program to Delete a Node From the Middle of the Circular Linked List In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C 4 min read How to Implement Generic LinkedList in Java? Linked List is Linear Data Structures that store values in nodes. As we do know here each Node possesses two properties namely the value of the node and link to the next node if present so. Linked List can not only be of Integer data type but String, boolean, Float, Character, etc. We can implement 8 min read Java Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to 6 min read Like