def rescue_walk(algns1, algns2, max_molecule_size):
    """
    Rescue a single ligation that appears as a walk.

    Checks if a molecule with three alignments could be formed via a single 
    ligation between two fragments, where one fragment was so long that it 
    got sequenced on both sides.

    Uses three criteria:
    a) the 3'-end alignment on one side maps to the same chromosome as the 
    alignment fully covering the other side (i.e. the linear alignment)
    b) the two alignments point towards each other on the chromosome
    c) the distance between the outer ends of the two alignments is below
    the specified threshold.


    Alternatively, a single ligation get rescued when the 3' sub-alignment 
    maps to multiple locations or no locations at all.
    
    In the case of a successful rescue, tags the 3' sub-alignment with
    type='X' and the linear alignment on the other side with type='R'.

    Returns
    -------
    linear_side : int
        If the case of a successful rescue, returns the index of the side
        with a linear alignment.

    """
    n_algns1 = len(algns1)
    n_algns2 = len(algns2)
    if n_algns1 <= 1 and n_algns2 <= 1:
        return None
    if not (n_algns1 == 1 and n_algns2 == 2 or n_algns1 == 2 and n_algns2 == 1
        ):
        return None
    first_read_is_chimeric = n_algns1 > 1
    chim5_algn = algns1[0] if first_read_is_chimeric else algns2[0]
    chim3_algn = algns1[1] if first_read_is_chimeric else algns2[1]
    linear_algn = algns2[0] if first_read_is_chimeric else algns1[0]
    if not (linear_algn['is_mapped'] and linear_algn['is_unique']):
        return None
    can_rescue = True
    if chim3_algn['is_mapped'] and chim5_algn['is_unique']:
        can_rescue &= chim3_algn['chrom'] == linear_algn['chrom']
        can_rescue &= chim3_algn['strand'] != linear_algn['strand']
        if linear_algn['strand'] == '+':
            can_rescue &= linear_algn['pos5'] < chim3_algn['pos5']
        else:
            can_rescue &= linear_algn['pos5'] > chim3_algn['pos5']
        if linear_algn['strand'] == '+':
            molecule_size = chim3_algn['pos5'] - linear_algn['pos5'
                ] + chim3_algn['dist_to_5'] + linear_algn['dist_to_5']
        else:
            molecule_size = linear_algn['pos5'] - chim3_algn['pos5'
                ] + chim3_algn['dist_to_5'] + linear_algn['dist_to_5']
        can_rescue &= molecule_size <= max_molecule_size
    if can_rescue:
        if first_read_is_chimeric:
            algns1[1]['type'] = 'X'
            algns2[0]['type'] = 'R'
            return 2
        else:
            algns1[0]['type'] = 'R'
            algns2[1]['type'] = 'X'
            return 1
    else:
        return None
