Reverse Linked List

Problem Summary

(Write in your own words, not copied from LeetCode. This forces comprehension.)

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev, cur = None, head

        while cur:
            next = cur.next
            cur.next = prev
            prev, cur = cur, next

        return prev

Common Mistakes / Things I Got Stuck On