nathanwailes

Linked List - Merge two sorted lists

Aug 21st, 2024 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.64 KB | None | 0 0
  1. """
  2. https://leetcode.com/problems/merge-two-sorted-lists/description/
  3.  
  4. You are given the heads of two sorted linked lists list1 and list2.
  5.  
  6. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
  7.  
  8. Return the head of the merged linked list.
  9.  
  10.  
  11. 2024.08.21
  12. - The way I remembered how to write the code was to start with the `if curr1.val <= curr2.val:` check. I then wrote
  13. the stuff in the block: `current.next = curr1; curr1 = curr1.next`. And then it was clear I needed to add the `else:`
  14. case.
  15. - I think at this point it became clear that this was very similar to the mergesort pattern of handling the case
  16. where you're drawing from two different lists, then needing to handle the case where one of the lists still has items
  17. in it when the other has run out. And I remembered the explanation that I could just append the remaining node
  18. without needing to iterate through its following nodes.
  19. - The last thing to remember was to create a dummy head node. I'm not sure of a way to arrive at this by first
  20. principles in an interview setting, I just had it memorized.
  21. - I got an error on a test case because I didn't initialize the dummy head node to have an initial value.
  22. """
  23.  
  24. # Definition for singly-linked list.
  25. # class ListNode:
  26. #     def __init__(self, val=0, next=None):
  27. #         self.val = val
  28. #         self.next = next
  29.  
  30.  
  31. # Iterative solution
  32. """
  33. 2024.08.21
  34. - The way I remembered how to write the code was to start with the `if curr1.val <= curr2.val:` check. I then wrote
  35. the stuff in the block: `current.next = curr1; curr1 = curr1.next`. And then it was clear I needed to add the `else:`
  36. case.
  37. - I think at this point it became clear that this was very similar to the mergesort pattern of handling the case
  38. where you're drawing from two different lists, then needing to handle the case where one of the lists still has items
  39. in it when the other has run out. And I remembered the explanation that I could just append the remaining node
  40. without needing to iterate through its following nodes.
  41. - The last thing to remember was to create a dummy head node. I'm not sure of a way to arrive at this by first
  42. principles in an interview setting, I just had it memorized.
  43. - I got an error on a test case because I didn't initialize the dummy head node to have an initial value.
  44. """
  45.  
  46. class Solution:
  47.     def mergeTwoLists(self, l1, l2):
  48.         prehead = ListNode(-1)
  49.         curr = prehead
  50.         while l1 and l2:
  51.             if l1.val <= l2.val:
  52.                 curr.next = l1
  53.                 l1 = l1.next
  54.             else:
  55.                 curr.next = l2
  56.                 l2 = l2.next
  57.             curr = curr.next
  58.         curr.next = l1 if l1 else l2
  59.         return prehead.next
  60.  
  61.  
  62.  
  63.  
  64. # Recursive Solution
  65. """
  66. I think the way to remember this one is to remember that the base case is if one of the nodes is None. Once you write out those two if conditions, it becomes clear that the recursive case is when you have two nodes and need to decide which one is going to be the head of the returned list. So you then write out the `l1.val <= l2.val` check and then the `return l1` line, and it becomes clear that the missing piece is `l1.next = ...` and you just need to fill in the `...` part.
  67. """
  68. class Solution:
  69.     def mergeTwoLists(self, l1, l2):
  70.         # base case
  71.         if not l1:
  72.             return l2
  73.         elif not l2:
  74.             return l1
  75.        
  76.         # recursive case
  77.         if l1.val < l2.val:
  78.             l1.next = self.mergeTwoLists(l1.next, l2)
  79.             return l1
  80.         else:
  81.             l2.next = self.mergeTwoLists(l1, l2.next)
  82.             return l2
Advertisement
Add Comment
Please, Sign In to add comment