forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1721 (doocs#1846)
No.1721.Swapping Nodes in a Linked List
- Loading branch information
Showing
8 changed files
with
228 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
54 changes: 28 additions & 26 deletions
54
solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
/** | ||
* 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* swapNodes(ListNode* head, int k) { | ||
ListNode* p1 = head; | ||
for (int i = 1; i < k; i++) | ||
p1 = p1->next; | ||
ListNode *slow = head, *fast = p1->next; | ||
|
||
while (fast) { | ||
fast = fast->next; | ||
slow = slow->next; | ||
} | ||
swap(slow->val, p1->val); | ||
return head; | ||
} | ||
}; | ||
/** | ||
* 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* swapNodes(ListNode* head, int k) { | ||
ListNode* fast = head; | ||
while (--k) { | ||
fast = fast->next; | ||
} | ||
ListNode* slow = head; | ||
ListNode* p = fast; | ||
while (fast->next) { | ||
fast = fast->next; | ||
slow = slow->next; | ||
} | ||
ListNode* q = slow; | ||
swap(p->val, q->val); | ||
return head; | ||
} | ||
}; |
30 changes: 30 additions & 0 deletions
30
solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* public int val; | ||
* public ListNode next; | ||
* public ListNode(int val=0, ListNode next=null) { | ||
* this.val = val; | ||
* this.next = next; | ||
* } | ||
* } | ||
*/ | ||
public class Solution { | ||
public ListNode SwapNodes(ListNode head, int k) { | ||
ListNode fast = head; | ||
while (--k > 0) { | ||
fast = fast.next; | ||
} | ||
ListNode p = fast; | ||
ListNode slow = head; | ||
while (fast.next != null) { | ||
fast = fast.next; | ||
slow = slow.next; | ||
} | ||
ListNode q = slow; | ||
int t = p.val; | ||
p.val = q.val; | ||
q.val = t; | ||
return head; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.