Reverse Nodes in k-Group
LeetCode 25 · ಕಠಿಣ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Find kth node → detach → reverse → reconnect.
ನಾನು Reverse Nodes in k-Group (LeetCode 25) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Reversal ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಕಠಿಣ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Process the list in chunks of k. For each full chunk, cut it off, reverse it, and reconnect: the previous chunk's tail points to this chunk's new head, and this chunk's old head (now its tail) points to whatever comes next. A short final chunk is left alone.
Approach
- 1
getKthNode finds the end of the current group (None if too short).
- 2
Save next_node, cut, reverse the group.
- 3
First group sets the new head; later groups link from prev_node.
- 4
Old group head (now tail) links to next_node; move on.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ
Iterative group reversalPythonO(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 reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
#get kth node
#have reference of prev node and next node
#only in - first case prev node will be head
#but second case prev node will be the node to rewire with second node
#have temp to have the pointer
def getKthNode(node,k):
k-=1
while k!=0 and node!=None:
node = node.next
k-=1
return node
def reverse_list(node):
#prev temp front
#. prev temp
prev = None
temp = node
while temp!=None:
front = temp.next
temp.next = prev
prev = temp
temp = front
return temp
temp=head
prev_node = None
next_node = None
while temp!=None:
kthNode = getKthNode(temp,k)
if kthNode==None:
break
next_node = kthNode.next
kthNode.next=None
reverse_list(temp)
if temp==head:
head = kthNode
else:
prev_node.next = kthNode #prev node was changed to 1 because it was assigned with temp after first iteration
temp.next = next_node #3.next = 5
prev_node = temp #prev_node = 3
temp = next_node #temp=5
return headReverse Nodes in k-Group — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Reverse Nodes in k-Group ಹಿಂದಿನ intuition ಏನು?
Process the list in chunks of k. For each full chunk, cut it off, reverse it, and reconnect: the previous chunk's tail points to this chunk's new head, and this chunk's old head (now its tail) points to whatever comes next. A short final chunk is left alone.
Reverse Nodes in k-Group (LeetCode 25) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. getKthNode finds the end of the current group (None if too short). 2. Save next_node, cut, reverse the group. 3. First group sets the new head; later groups link from prev_node. 4. Old group head (now tail) links to next_node; move on.
Reverse Nodes in k-Group ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(n). Space: O(1). (Accepted approach: Iterative group reversal.)
Reverse Nodes in k-Group ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Track prev_node across groups so they stay connected.