Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
  2.  
  3. * solution1: Recursion
  4. ```python3
  5. # -*- coding:utf-8 -*-
  6. # class ListNode:
  7. # def __init__(self, x):
  8. # self.val = x
  9. # self.next = None
  10. class Solution:
  11. # 返回合并后列表
  12. def Merge(self, pHead1, pHead2):
  13. # write code here
  14. if not pHead1 or not pHead2:
  15. return pHead1 or pHead2
  16. elif pHead1.val < pHead2.val:
  17. pHead1.next = self.Merge(pHead1.next,pHead2)
  18. return pHead1
  19. else:
  20. pHead2.next = self.Merge(pHead1,pHead2.next)
  21. return pHead2
  22. ```
  23. * solution2: Dummy + Iteratively
  24. ```python3
  25. # -*- coding:utf-8 -*-
  26. # class ListNode:
  27. # def __init__(self, x):
  28. # self.val = x
  29. # self.next = None
  30. class Solution:
  31. # 返回合并后列表
  32. def Merge(self, pHead1, pHead2):
  33. # write code here
  34. dummy =
  35. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement