leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 79a71b7fa31283cf5540f058578422ca2c48e0f5
parent 5cba8c27830fb8d44ab396f67f4bf325304d7bd7
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 15 May 2023 21:12:21 +0200

Daily Problem

Diffstat:
AProblems/1721.cpp | 14++++++++++++++
MREADME.md | 1+
2 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/Problems/1721.cpp b/Problems/1721.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + ListNode *swapNodes(ListNode *head, int k) { + ListNode *tmp = head, *first; + while (--k) tmp = tmp->next; + first = tmp; + + ListNode *second = head; + tmp = tmp->next; + while (tmp) tmp = tmp->next, second = second->next; + swap(first->val, second->val); + return head; + } +}; diff --git a/README.md b/README.md @@ -476,6 +476,7 @@ for solving problems. | 1700 | Easy | [Number of Students Unable to Eat Lunch](Problems/1700.cpp) | | 1704 | Easy | [Determine if String Halves Are Alike](Problems/1704.cpp) | | 1706 | Medium | [Where Will the Ball Fall](Problems/1706.cpp) | +| 1721 | Medium | [Swapping Nodes in a Linked List](Problems/1721.cpp) | | 1722 | Medium | [Minimize Hamming Distance After Swap Operations](Problems/1722.cpp) | | 1768 | Easy | [Merge Strings Alternately](Problems/1768.cpp) | | 1786 | Medium | [Number of Restricted Paths From First to Last Node](Problems/1786.cpp) |