单链表反转
class Node(object): def __init__(self, data, next): self.data = data self.next = next def reverse(head): if head is None or head.next is None: return head pre = None current = head while current: tmp = current current = current.next tmp.next = pre pre = tmp print_node(pre) return pre def print_node(head): result = "" while head: result += str(head.data)+"->" head = head.next print result if __name__ == "__main__": h = Node(1, Node(2, Node(3, Node(4, Node(5, None))))) h1 = reverse(h)