Given the head of a singly linked list, reverse the list, and return the reversed list.
Example:
Input: head =[1, 2, 3, 4, 5]
Output:[5, 4, 3, 2, 1]
Your algorithm should run in O(n) time and use O(1) extra space.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(head: ListNode) -> ListNode:
# Initialize previous node to None
prev = None
# Initialize current node to head
current = head
# Iterate through the list
while current:
# Save the next node
next_node = current.next
# Reverse the current node's pointer
current.next = prev
# Move prev and current one step forward
prev = current
current = next_node
# Return the new head of the reversed list
return prev
## Complexity Analysis
### Time Complexity
- The algorithm runs in O(n) time, where n is the number of nodes in the linked list. This is because we iterate through the list once.
### Space Complexity
- The algorithm uses O(1) extra space, as we only use a few pointers for the reversal process.
## Edge Cases
### Empty List
- If the input list is empty (`head` is `None`), the function should return `None`.
### Single Node List
- If the list contains only one node, the function should return the same node as the head of the reversed list.
### Testing
To test the solution comprehensively, consider the following test cases:
1. An empty list.
2. A list with a single node.
3. A list with multiple nodes.
4. A list with negative values.
5. A list with duplicate values.
### Example Test Cases
python
# Helper function to create a linked list from a list
def create_linked_list(arr):
if not arr:
return None
head = ListNode(arr[0])
current = head
for val in arr[1:]:
current.next = ListNode(val)
current = current.next
return head
# Helper function to convert a linked list to a list
def linked_list_to_list(head):
result = []
current = head
while current:
result.append(current.val)
current = current.next
return result
# Test cases
head = create_linked_list([1, 2, 3, 4, 5])
reversed_head = reverseList(head)
print(linked_list_to_list(reversed_head)) # Output: [5, 4, 3, 2, 1]
head = create_linked_list([])
reversed_head = reverseList(head)
print(linked_list_to_list(reversed_head)) # Output: []
head = create_linked_list([1])
reversed_head = reverseList(head)
print(linked_list_to_list(reversed_head)) # Output: [1]
## Thinking and Problem-Solving Tips
1. **Understand the Problem**: Break down the problem into smaller parts and understand the requirements.
2. **Visualize**: Draw diagrams to visualize the linked list and the reversal process.
3. **Iterate and Improve**: Start with a simple solution and iteratively improve it.
4. **Practice**: Solve similar problems to strengthen your understanding and skills.
## Conclusion
Reversing a linked list is a fundamental problem that helps in understanding linked list manipulations. By using an iterative approach with three pointers, we can achieve an efficient solution with O(n) time complexity and O(1) space complexity. Practice and understanding of such problems are crucial for mastering data structures and algorithms.
## Additional Resources
- [LeetCode - Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
- [GeeksforGeeks - Reverse a Linked List](https://www.geeksforgeeks.org/reverse-a-linked-list/)
- [Python Documentation - Data Structures](https://docs.python.org/3/tutorial/datastructures.html)
Happy coding!
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE