nathanwailes

Linked List - Reverse a linked list

Aug 21st, 2024 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. """
  2. https://leetcode.com/problems/reverse-linked-list/description/
  3.  
  4. Given the head of a singly linked list, reverse the list, and return the reversed list.
  5.  
  6. 2024.08.21
  7. - Ok I think the way I remembered how to do this is useful for the future. The first line of code
  8. that I wrote in the function was `current.next = prev`, which is IMO the core of the algorithm, where you're flipping
  9. around the order of the connections.
  10. - That line of code reminded me that I need to have `current` and `prev` variables. The next line of code I wrote was
  11. `prev = current`, because that's the other core of the algorithm IMO, the step forward.
  12. - I then saw that I would clearly need to update the `current` variable, which would be the current node's original
  13. `.next`, but since I was changing it with the `current.next = prev` line, I'd need to save a reference to the original
  14. node. So I wrote the `current = _next` line followed by the `_next = current.next` line.
  15. - The rest is easy, I just added the `while current:`, the initial `current = ` and `prev = `, and it was clear that
  16. `prev` would have a reference to the final node in the original list (which becomes the head node in the reordered
  17. list).
  18. """
  19.  
  20. # Definition for singly-linked list.
  21. # class ListNode:
  22. #     def __init__(self, val=0, next=None):
  23. #         self.val = val
  24. #         self.next = next
  25.  
  26.  
  27. # Iterative solution
  28. class Solution:
  29.     def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
  30.         current = head
  31.         prev = None
  32.         while current:
  33.             _next = current.next
  34.             current.next = prev
  35.             prev = current
  36.             current = _next
  37.         return prev
  38.  
  39.  
  40. # Recursive solution
  41.  
  42. """
  43. The `head is None` check is just handling the single edge-case of getting passed zero nodes at the beginning (i.e. empty input). If any nodes at all are passed in the original function, that check should never get reached, as the recursion will end at the `head.next is None` check first. This is important to understand because it's how we can be sure that the recursive function will return an actual node as the head of the reversed list instead of None (which would seem to be the natural endpoint for recursively looking at the .next property in a linked list).
  44.  
  45. I think the `head.next.next = head` line is the key trick to remember for this function.
  46. """
  47.  
  48. class Solution:
  49.     def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
  50.         if head.next is None or head is None:
  51.             return head
  52.        
  53.         head_of_reversed_list = self.reverseList(head.next)
  54.         head.next.next = head
  55.         head.next = None
  56.  
  57.         return head_of_reversed_list
Advertisement
Add Comment
Please, Sign In to add comment