Advertisement
Guest User

Untitled

a guest
May 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. # use two dummy nodes to store the nodes which are smaller than the key value
  2. # and the larger nodes
  3. # concatenate the two linked lists by removing the dummy nodes
  4. # O(n) in time, O(1) in space
  5. def solution(ll, key):
  6. dummyNode1 = Node()
  7. dummyNode2 = Node()
  8. p1 = dummyNode1
  9. p2 = dummyNode2
  10. currNode = ll
  11. while currNode != None:
  12. if currNode.value < key:
  13. p1.next = currNode
  14. p1 = p1.next
  15. currNode = currNode.next
  16. else:
  17. p2.next = currNode
  18. p2 = p2.next
  19. currNode = currNode.next
  20. p1.next = dummyNode2.next
  21. return dummyNode1.next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement