🧠
Recursive Approach with Accumulated Value
💡 This approach uses recursion to accumulate the integer value as we traverse the linked list, helping understand recursion and how to carry state through calls.
Intuition
Recursively process the current node by shifting the accumulated value so far left by 1 and adding the current node's value, then recurse to the next node.
Algorithm
- Define a helper recursive function that takes the current node and the accumulated integer value so far.
- If the node is null, return the accumulated value.
- Update the accumulated value by shifting it left by 1 and adding the current node's value.
- Recursively call the helper function on the next node with the updated accumulated value.
- Return the final accumulated value.
💡 The challenge is understanding how to carry the accumulated value through recursive calls instead of building from the end.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def getDecimalValue(head: ListNode) -> int:
def helper(node, acc):
if node is None:
return acc
acc = (acc << 1) | node.val
return helper(node.next, acc)
return helper(head, 0)
# Example usage:
if __name__ == '__main__':
node3 = ListNode(1)
node2 = ListNode(0, node3)
node1 = ListNode(1, node2)
print(getDecimalValue(node1)) # Output: 5
Line Notes
def helper(node, acc):Define a recursive helper function with current node and accumulated value
if node is None:Base case: if node is null, return the accumulated value
acc = (acc << 1) | node.valShift accumulated value left and add current node's bit
return helper(node.next, acc)Recurse to next node with updated accumulated value
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
public class Solution {
public static int getDecimalValue(ListNode head) {
return helper(head, 0);
}
private static int helper(ListNode node, int acc) {
if (node == null) return acc;
acc = (acc << 1) | node.val;
return helper(node.next, acc);
}
public static void main(String[] args) {
ListNode node3 = new ListNode(1);
ListNode node2 = new ListNode(0);
node2.next = node3;
ListNode node1 = new ListNode(1);
node1.next = node2;
System.out.println(getDecimalValue(node1)); // Output: 5
}
}
Line Notes
return helper(head, 0);Start recursion with head node and initial accumulated value 0
if (node == null) return acc;Base case: if node is null, return accumulated value
acc = (acc << 1) | node.val;Shift accumulated value left and add current node's bit
return helper(node.next, acc);Recurse to next node with updated accumulated value
#include <iostream>
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
int helper(ListNode* node, int acc) {
if (node == nullptr) return acc;
acc = (acc << 1) | node->val;
return helper(node->next, acc);
}
int getDecimalValue(ListNode* head) {
return helper(head, 0);
}
int main() {
ListNode* node3 = new ListNode(1);
ListNode* node2 = new ListNode(0);
node2->next = node3;
ListNode* node1 = new ListNode(1);
node1->next = node2;
std::cout << getDecimalValue(node1) << std::endl; // Output: 5
return 0;
}
Line Notes
int helper(ListNode* node, int acc) {Recursive helper function with current node and accumulated value
if (node == nullptr) return acc;Base case: if node is null, return accumulated value
acc = (acc << 1) | node->val;Shift accumulated value left and add current node's bit
return helper(node->next, acc);Recurse to next node with updated accumulated value
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function getDecimalValue(head) {
function helper(node, acc) {
if (node === null) return acc;
acc = (acc << 1) | node.val;
return helper(node.next, acc);
}
return helper(head, 0);
}
// Example usage:
const node3 = new ListNode(1);
const node2 = new ListNode(0, node3);
const node1 = new ListNode(1, node2);
console.log(getDecimalValue(node1)); // Output: 5
Line Notes
function helper(node, acc) {Recursive helper function with current node and accumulated value
if (node === null) return acc;Base case: if node is null, return accumulated value
acc = (acc << 1) | node.val;Shift accumulated value left and add current node's bit
return helper(node.next, acc);Recurse to next node with updated accumulated value
We traverse the linked list once, but recursion uses call stack proportional to n.
💡 For n=20, this means 20 recursive calls which might risk stack overflow in some environments.
Interview Verdict: Accepted but risks stack overflow
Good for demonstrating recursion but not recommended for very large inputs due to stack depth.