#!/usr/bin/env python # Demonstrate creation of a linked list and then its reversal # Copyright (c) 2005 TundraWare Inc., Des Plaines, IL # All Rights Reserved # Last Modified: 2-15-2005 # A linked list element class class element(object): payload = None next = None # Print out the payloads in linked order def PrintList(head): if not head: print "Bogus: Null Head Of List Passed!!!" return head current = head while current: print current.payload current = current.next print " " # separator # Reverse the order of the list, returning a new head def ReverseList(head): current = head previous = None while current.next: next = current.next current.next = previous previous = current current = next else: current.next = previous return current ##### # Main Program Entry point ##### # Create a linked list head = element() head.payload = "OriginalHead" last = head for index in range(10): new = element() new.payload = index last.next = new last = new PrintList(head) head = ReverseList(head) PrintList(head)