diff --git a/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp new file mode 100644 index 000000000..e23c88580 --- /dev/null +++ b/05- May/15- Swapping Nodes in a Linked List/15- Swapping Nodes in a Linked List ( Ibrahim Khalid ).cpp @@ -0,0 +1,29 @@ +// Author : Ibarhim Khalid +class Solution { +public: + ListNode* swapNodes(ListNode* head, int k) { + // to store values + vectorv; + 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<val<<" "; + tmp=tmp->next; + in++; + } + + return head; + } +};