Skip to content

Commit

Permalink
Merge pull request 7oSkaaa#1093 from Hima-khalid/main
Browse files Browse the repository at this point in the history
15 th day
  • Loading branch information
7oSkaaa committed May 15, 2023
2 parents ffa77d9 + 218decc commit efda285
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Author : Ibarhim Khalid
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
// to store values
vector<int>v;
int cnt=0;
ListNode *temp = head;
// move between values in linkedlist and store it in vector
while(temp !=NULL){
v.push_back(temp->val);
temp=temp->next;
}
// swapping the values of the kth node from the beginning and the kth node from the end
swap(v[k-1],v[v.size()-k]);
// pointer to index in vector
int in=0;
ListNode *tmp = head;
// reorder linkedlist by modified vector
while(tmp !=NULL){
tmp->val=v[in];
cout<<tmp->val<<" ";
tmp=tmp->next;
in++;
}

return head;
}
};

0 comments on commit efda285

Please sign in to comment.