Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Zip odd, even nodes solution in Clear category for Swap Nodes by kkkkk
def swap_nodes(nodes):
"""Swap elements in list pairwise."""
swapped_list = [item for t in zip(nodes[0::2], nodes[1::2])
for item in reversed(t)]
# If the list has an odd number of nodes, add the last (odd)
# node back into the list.
if len(nodes) % 2:
swapped_list.append(nodes[-1])
return swapped_list
Aug. 22, 2019