Coming from 61A (Python) to 61B (Java), one of the first moments of friction is: "where do I put my helper function?" In Python, you'd nest it right inside the outer function without a second thought. In Java, your TA gives you a slightly hesitant answer — and for good reason. The two languages have fundamentally different philosophies about where "helper" logic lives.
The core shift: Python is function-centric. Java is class-centric. Everything in Java must belong to a class — including your helpers.
In 61A, writing a helper was effortless:
def add(self, val):
def helper(node, val): # ← lives right here, no fuss
if node is None:
return Link(val)
node.rest = helper(node.rest, val)
return node
self.head = helper(self.head, val)
Python's nested functions are lightweight and come with a superpower: closures. The inner function automatically captures variables from the outer scope — no extra arguments needed.
Java does not let you define a method inside another method. This is a syntax error:
public void add(int val) {
private Node helper(Node n, int val) { // ← SYNTAX ERROR
...
}
}
This isn't a quirk you can work around — it's by design. Java is built around classes as the fundamental unit of organization.
The most direct substitute. Your helper becomes a private method on the class — still invisible to the outside world, just promoted one level up.
public void add(int val) {
head = addHelper(head, val); // call it
}
private Node addHelper(Node n, int val) {
if (n == null) return new Node(val);
n.next = addHelper(n.next, val);
return n;
}
Same logic, same encapsulation. This is what your TA meant by "the easy case."