mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
leetcode new problems
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
class Solution{
|
||||
public:
|
||||
ListNode* SwapNode(ListNode* head){
|
||||
if(!head || !head->next) return head;
|
||||
|
||||
ListNode* dummy = new ListNode(0);
|
||||
dummy->next = head;
|
||||
|
||||
ListNode* cur = head;
|
||||
ListNode* prev = dummy;
|
||||
|
||||
while(cur && cur->next){
|
||||
ListNode* nextPair = cur->next->next;
|
||||
ListNode* second = cur->next;
|
||||
|
||||
second->next = cur;
|
||||
cur->next = nextPair;
|
||||
prev->next = second;
|
||||
|
||||
prev = cur;
|
||||
cur = nextPair;
|
||||
}
|
||||
ListNode* newHead = dummy->next;
|
||||
delete dummy;
|
||||
return newHead;
|
||||
}
|
||||
};
|
||||
|
||||
//TC : O(n)
|
||||
//SC : O(1)
|
||||
Reference in New Issue
Block a user