Reverse Linked List
LeetCode 206 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
prev ← temp ← front, one arrow at a time.
நான் Reverse Linked List (LeetCode 206) ஐ இப்படித் தீர்த்தேன் — Reversal பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Walk the list and flip each arrow to point backwards. You need three references: the previous node (new next), the current node, and the original next (so you don't lose the rest of the list).
Approach
- 1
prev = None, temp = head.
- 2
front = temp.next; temp.next = prev.
- 3
prev = temp; temp = front.
- 4
prev is the new head.
நான் எப்படித் தீர்த்தேன்
Iterative pointer flipPythonO(n) time · O(1) space
# 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]:
temp = head
prev=None
front = None
while temp:
#prev temp front
#prev temp front
front = temp.next
temp.next = prev
prev=temp
temp=front
return prev
Reverse Linked List — அடிக்கடி கேட்கப்படும் கேள்விகள்
Reverse Linked List பின்னாலுள்ள intuition என்ன?
Walk the list and flip each arrow to point backwards. You need three references: the previous node (new next), the current node, and the original next (so you don't lose the rest of the list).
Reverse Linked List (LeetCode 206) ஐ எப்படித் தீர்ப்பது?
1. prev = None, temp = head. 2. front = temp.next; temp.next = prev. 3. prev = temp; temp = front. 4. prev is the new head.
Reverse Linked List இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(1). (Accepted approach: Iterative pointer flip.)
Reverse Linked List இல் பொதுவான தவறுகள் என்ன?
Save temp.next before overwriting it.