classListNode: def__init__(self,x): self.val = x self.next = None
defreverse(head): if head == Noneor head.next == None: return head pre = None cur = head h = head while cur: h = cur tmp = cur.next cur.next = pre pre = cur cur = tmp return h
head = None tmp = head for i in xrange(1,10): if head == None: head = ListNode(i) tmp = head else: tmp.next = ListNode(i) tmp = tmp.next tmp = head while tmp: print tmp.val tmp = tmp.next
head = reverse(head) print'----------' tmp = head while tmp: print tmp.val tmp = tmp.next