Determining if a palindrome exists in a linked list using recursion Summary of code:

This code recurses through half of the LinkedList, "adding" the left half node's to the call stack. Then, once at the middle, the code returns the first node of the right side (or more accurately, it assigns it to a variable, "right_node"). As the call stack collapses, the code compares the left nodes with the right nodes, and the right node is assigned to right node's next node.

The full code and updates are available here.

Specific questions:

I have a recursive function with two base cases, and both involve returning True, but each have to do a different step beforehand. Is there a better way to write out this if else statement structure?

This is the code snippet that features the if/else structure:

def recurse(head, length, node):
    if length == 1:  # odd number of nodes
        node[0] = head.next
    if length == 0:  # even number of nodes
        node[0] = head
    if length == 0 or length == 1:
        return True

    recurse(head.next, length/2, node)
Generally, when you end up with an expression like this, it is a sign that something is not right, and something needs rethinking or refactoring.

I don't see the point of node here - since you always write to node[0] -  and if you would eliminate it, that is, use it as the return value, your function would be a lot simpler:

def recurse(head, length):
    if length == 1:  # odd number of nodes
        return head.next
    elif length == 0:  # even number of nodes
        return head

    return recurse(head.next, length//2)


or, on one line

def recurse(head, length):
    return head if length==0 else head.next if length==1 else recurse(head.next, length//2)


BUT - 1//2 == 0, so both of these get a lot easier if you realize you have only one base case:

def recurse(head, length):
    if length == 0:
        return head

    return recurse(head.next, length//2)


or, on one line:

def recurse(head, length):
    return head if length==0 else recurse(head.next, length//2)


Of course, this might be better not to recurse:

def nonrecurse(head, length):
    while (length &gt; 0): 
        head = head.next
        length//=2

    return head