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
How to convert a binary linked list to decimal in Java
More Relevant Posts
-
#100DaysOfCode – Day 91 Delete All Occurrences in a Doubly Linked List Given a doubly linked list and a key x, the task is to remove every node whose value equals x, and return the updated list. Example: Input: 2 <-> 2 <-> 10 <-> 8 <-> 4 <-> 2 <-> 5 <-> 2 Key: 2 Output: 10 <-> 8 <-> 4 <-> 5 My Approach Traversed the list using a pointer temp. For every node where temp.data == x: If it’s the head, shifted the head forward. Otherwise, re-linked the previous and next nodes to bypass the current one. Continued until the end of the list. Time Complexity: O(N) Space Complexity: O(1) Working with doubly linked lists reinforces how important pointer handling is. A single wrong link can break the entire structure but clean logic leads to elegant solutions! #100DaysOfCode #Java #DSA #LinkedList #GeeksforGeeks #ProblemSolving #CodingJourney #TakeUForward #CodeNewbie #LearningEveryday
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 80 Reverse Linked List Problem: Given the head of a singly linked list, reverse the list and return the reversed version. Example: Input: [1, 2, 3, 4, 5] Output: [5, 4, 3, 2, 1] My Approach: Used an iterative method to reverse the list efficiently by manipulating pointers. Initialized three pointers prev, temp, and front. Iteratively reversed each node’s direction until the end of the list was reached. Returned prev as the new head of the reversed list. Time Complexity: O(N) Space Complexity: O(1) Reversing a linked list might look tricky at first, but once you understand pointer manipulation it’s pure logic and flow. #takeUforward #100DaysOfCode #DSA #Java #ProblemSolving #LeetCode #LinkedList #Pointers #CodeNewbie
To view or add a comment, sign in
-
-
💡 LeetCode #2824 — Count Pairs Whose Sum Is Less Than Target Today I solved LeetCode Problem 2824: Count Pairs Whose Sum Is Less Than Target 🔢 Problem Summary: Given an integer array nums and a number target, return the number of pairs (i, j) where i < j and nums[i] + nums[j] < target Key Idea: Use the two-pointer technique after sorting the array. Sort the array to make pair checking efficient. Keep one pointer at the start (i) and one at the end (j). If nums[i] + nums[j] is less than target, then all pairs between i and j are valid → add (j - i) to the count. Otherwise, move the right pointer left. This gives an O(n log n) solution due to sorting. #LeetCode #Java #DSA #TwoPointers #Sorting #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 29/100 – #100DaysOfCode 🚀 | #Java #LeetCode #BinaryTree ✅ Problem Solved: Verify Preorder Serialization of a Binary Tree 🌲 🧩 Problem Summary: Given a string representing a preorder serialization of a binary tree, determine if it’s valid. Example: "9,3,4,#,#,1,#,#,2,#,6,#,#" → ✅ valid "1,#" → ❌ invalid 💡 Approach Used: Used the slot-counting method 🧠 Each node uses one slot and non-null nodes create two new slots. Process the preorder string, ensuring slots never go negative and exactly zero remain at the end. ⚙️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Takeaway: This problem teaches how binary tree structure can be validated with simple counting logic — no need to rebuild the tree! 🌱 #Java #LeetCode #BinaryTree #100DaysOfCode #ProblemSolving #CodingChallenge
To view or add a comment, sign in
-
-
📌 Day 29/100 – Partition List (LeetCode 86) 🔹 Problem: Given the head of a linked list and a value x, rearrange the list so that: All nodes less than x come first Followed by nodes greater than or equal to x While preserving the original order within each group 🔹 Approach: I used the two-list technique: One list for nodes < x One list for nodes ≥ x Traverse once and attach nodes accordingly Finally, connect both lists This approach keeps the ordering intact and ensures an efficient O(n) solution. 🔹 Key Learnings: Dummy nodes simplify linked list manipulations a lot Maintaining order inside partitions needs careful pointer handling Merging lists becomes easy when structure is clear #100DaysOfCode #Day29 #LeetCode #Java #DSA #LinkedList #ProblemSolving #CodingJourney #KeepLearning
To view or add a comment, sign in
-
-
Day 46/100 Problem Statement : You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros. In one operation you can choose any subarray from initial and increment each value by one. Return the minimum number of operations to form a target array from initial. The test cases are generated so that the answer fits in a 32-bit integer. Input: target = [1,2,3,2,1] Output: 3 Solution : https://lnkd.in/gsXFvUr3 public int minNumberOperations(int[] target) { final int n=target.length; int ans=target[0]; for(int i=1; i<n; i++) ans+=Math.max(target[i]-target[i-1], 0); return ans; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF
To view or add a comment, sign in
-
#Day-68) LeetCode #3228 – Maximum Number of Operations to Move Ones to the End (Java Edition) Tackled this neat string problem using a greedy approach in Java. The challenge? Move '1's to the end of the string under specific movement rules, maximizing the number of valid operations. 🧠 Core Idea: Track how many '0's we've seen and how many '1's we've already moved. Only move a '1' if there's enough '0's to justify it and the next character is also '1'. 💻 Java Strategy: Loop through the string Use counters to manage '0's and '1's Let me know how you'd tweak this or if you see a more optimal path! #Java #LeetCode #GreedyAlgorithm #StringProblems #DSA #CodingChallenge #LinkedInTech #ProblemSolving
To view or add a comment, sign in
-
-
🚀Day 96/100 #100DaysOfLeetCode 🧩Problem: Reverse Linked List II✅ 💻Language: Java 💡 Approach: 1️⃣ First, use a dummy node to handle edge cases where reversal starts at the head. 2️⃣ Traverse to the node just before the left position — call it prev. 3️⃣ Reverse the sublist between left and right using standard pointer manipulation. 4️⃣ Reconnect the reversed portion back into the original list. 🔑Key Takeaways: 🔹Dummy nodes simplify linked list edge cases. 🔹In-place reversal reduces memory overhead. 🔹Careful pointer tracking ensures list integrity. ⚙️Performance: ⏱️Runtime: 0 ms(beats 100.00%) 💾Memory: 41.29 MB(beats 70.37%) #100DaysOfLeetCode #Java #LinkedList #CodingJourney #ProblemSolving #DSA #LeetCode #CodingChallenge
To view or add a comment, sign in
-
-
𝗜𝗳 𝘆𝗼𝘂 𝘂𝘀𝗲 𝗝𝗥𝗘... 𝘄𝗵𝘆 𝗱𝗼 𝘆𝗼𝘂 𝘀𝘁𝗶𝗹𝗹 𝗻𝗲𝗲𝗱 𝗝𝗩𝗠? Most devs get this backward. Let’s fix it once and for all. You don’t 𝘳𝘶𝘯 Java with JRE. You 𝘳𝘶𝘯 Java 𝗶𝗻𝘀𝗶𝗱𝗲 the JVM. The 𝗝𝗥𝗘 is just the environment — it gives the 𝗝𝗩𝗠 what it needs to do its job: standard libraries, config files, runtime support. Think of it like this 👇 • 𝗝𝗩𝗠 → the engine • 𝗝𝗥𝗘 → the car that holds the engine • 𝗝𝗗𝗞 → the factory that builds the car You don’t drive an engine. You drive a car. The car uses the engine to move. Same with Java. The 𝗝𝗥𝗘 𝘂𝘀𝗲𝘀 𝘁𝗵𝗲 𝗝𝗩𝗠 to run your code. Once you get this mental model, Everything about Java’s ecosystem suddenly clicks. #java #jre #jvm
To view or add a comment, sign in
-
-
Day 11: Two Sum Question appears to be easy but to make it more optimized we use another approach. Link of the problem: https://lnkd.in/gT94xrG8 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. Approach we use: Use Hashmap. The reason behind it is the key value pair we can use to make the question less complex. The soln is given below. Time Complexity: O(N) Space Complexity: O(N) DSA approach: One pass HashMap #Java #DSA #100DaysChallenge #ProblemSolving
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development