we can also use this recursive approach : class Solution { public: ListNode* swapPairs(ListNode* head) { int k = 2; ListNode* temp = head; int count = 0; // Step 1: Check if we have at least k nodes while (count < k && temp != nullptr) { temp = temp->next; count++; } // If we have fewer than k nodes, return head (no reversal) if (count < k) return head; // Step 2: Reverse k nodes ListNode* prev = swapPairs(temp); // Recurse for the rest of the list temp = head; int cnt = 0; while (cnt < k) { ListNode* next = temp->next; temp->next = prev; prev = temp; temp = next; cnt++; } return prev; } };
i have done this question on my own i have finally succeed
I accomplished it! I just had to watch the last part once. This part: while (count < k) { ListNode* next = temp->next; temp->next = prevNode; prevNode = temp; temp = next; count++; }
Your way of explaining is awesome Didi, You explain difficult questions in a very simple way.
React js course when coming plz replay......
please continue the DSA series
Done π Thank you Didi π
Awsome explaination
You're an awesome educator di!!
Great lecture π₯°ππ₯°ππ₯°π₯π₯π₯πππ₯°π₯°π₯°π₯°π
Continuity go slowly, But go High β€
Done Today's Lectureβ€
Thanku you very much for this DSA series
thank you didi ...πππ
plz continue the series plz
Lecture Completed!
Hello Didi , i'm from Bangladesh . Since you began creating videos for the DSA course using C + +, I have been viewing your videos on a regular basis. I also gained a lot of fresh knowledge. If I wish to enrol in your course, what can I do?
This question can be solved with same algorithm discuss in previous video(leetcode 25) with k=2.. Relpy if and edge case stucksπππ
Please upload the next video
@shradhaKD