Skip to content

Commit

Permalink
Add solution to day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed May 15, 2023
1 parent 041e726 commit 3082a9a
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Author: Ahmed Gamal

// this is a simple implementation problem
// we will use two pointers to point to the kth from the beginning and the kth from the end
// then we will swap them
// to get the kth from the end, we will use the size of the list - k + 1
// to get the size of the list, we will iterate over the list and count the number of nodes

/**
* 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) {
// size: the size of the list
// i: the kth node from the beginning
// j: the kth node from the end
int size = 0;
ListNode *i;
auto j = head;

// we will iterate over the list and count the number of nodes
while(j) {
size++;

// if we reached the kth node from the beginning, we will save it in i
if(size == k) {
i = j;
}
j = j -> next;
}

// we will get the kth node from the end (size - k + 1)
size -= k;
j = head;
while(size--) {
j = j -> next;
}

// we will swap the values of the two nodes
swap(i -> val, j -> val);

// we will return the head of the list
return head;
}
};

0 comments on commit 3082a9a

Please sign in to comment.