Created
June 27, 2014 19:25
-
-
Save yokiy/e60fc4079f4b7f9d3443 to your computer and use it in GitHub Desktop.
cc 2.4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#2.4 prtition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. | |
#creat two list, <x & >= x, merge them at last | |
from LinkedList import LinkedList, Node | |
def partition(self, x): | |
if self is None: | |
return | |
small = LinkedList() | |
large = LinkedList() | |
'''create two list''' | |
tmp = self.head | |
while tmp.next is not None: | |
if tmp.data < x: | |
small.AddNode(tmp.data) | |
else: | |
large.AddNode(tmp.data) | |
tmp = tmp.next | |
if self.end.data < x: | |
small.AddNode(self.end.data) | |
else: | |
large.AddNode(self.end.data) | |
small.end.next = large.head | |
print 'list partition with', x | |
small.printList() | |
# print 'large' | |
# large.printList() | |
'''test''' | |
test = LinkedList() | |
test.AddNode(3) | |
test.AddNode(16) | |
test.AddNode(25) | |
test.AddNode(8) | |
test.AddNode(15) | |
test.AddNode(20) | |
print 'the original list:' | |
test.printList() | |
partition(test, 16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment