Implement swapPairs function to swap nodes#272
Open
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Open
Implement swapPairs function to swap nodes#272aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideIntroduced a new C++ implementation of swapPairs using an iterative dummy-head approach that performs in-place pairwise node swaps in a singly-linked list. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: 24.Swap Nodes in Pairs.cpp
💡 Intuition
The core idea is to perform a group-wise reversal of linked list nodes, where each group has a size of two. Since the node pointers must be correctly updated to maintain the list structure, an iterative approach using a dummy node simplifies the logic, especially the handling of the new head and linking the swapped pair back to the previous segment of the list.
✍️ Approach
This solution uses an iterative approach with a dummy head node and three pointers to manage the swapping: prev, cur (the first node of the pair), and second (the second node of the pair).
Dummy Node Initialization: A dummy node is created and points to the original head. This node acts as a fixed starting point and simplifies the logic for updating the pointer that precedes the swapped pair. The pointer prev is initialized to point to the dummy node.
Iteration Setup: The pointer cur is initialized to the head of the list. The loop continues as long as a valid pair exists: while (cur && cur->next).
Swapping Steps (within the loop):
Identify: Identify the critical nodes:
second: The second node of the pair (cur->next).
npn (Next Pair Node): The first node of the next pair (cur->next->next). This is stored so we don't lose the rest of the list.
Reverse Pointers:
Link the second node to the first: second->next = cur; (Swap)
Link the original first node to the next unswapped segment: cur->next = npn; (Maintain continuity)
Link the preceding node to the new first node (which is second): prev->next = second; (Attach the swapped pair)
Advance Pointers:
prev moves to the unswapped node (cur), which is now the end of the swapped pair.
cur moves to the start of the next pair (npn).
Return: After the loop completes, the new head of the list is dummy.next.
Code Solution (C++)
C++
/**
Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
/
class Solution {
public:
ListNode swapPairs(ListNode* head) {
// Create a dummy node to act as the predecessor to the head
ListNode dummy(0, head);
ListNode *prev = &dummy;
ListNode *cur = head;
}
};
🔗 Related Issues
By submitting this PR, I confirm that:
[x] This is my original work not totally AI generated
[x] I have tested the solution thoroughly on leetcode
[x] I have maintained proper PR description format
[x] This is a meaningful contribution, not spam
Summary by Sourcery
New Features: