Given a singly linked list of size N, and an integer K. You need to swap the Kth node from beginning and Kth node from the end in the linked list. - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Thursday 9 July 2020

Given a singly linked list of size N, and an integer K. You need to swap the Kth node from beginning and Kth node from the end in the linked list.

The task is to complete the function swapkthnode(), which has arguments head, num(no of nodes), and K, and it should return a new head.
The task is to complete the function swapkthnode(), which has arguments headnum(no of nodes), and K, and it should return a new head. 

Given a singly linked list of size N, and an integer K. You need to swap the Kth node from beginning and Kth node from the end in the linked list.

Note: You need to swap the nodes through the links and not changing the content of the nodes.


The task is to complete the function swapkthnode(), which has arguments headnum(no of nodes), and K, and it should return a new head. 

The validation is done internally by the driver code to ensure that the swapping is done by changing references/pointers only.  A correct code would always cause output as 1.

Example:
Input:

3
4 1
1 2 3 4
5 3
1 2 3 4 5
4 4
1 2 3 4

Output:
1
1
1

#include <iostream>

using namespace std;

struct Node {
    int data;
    struct Node * next;
    Node(int x) {
        data = x;
        next = NULL;
    }
};



Node * swapkthnode(Node * head, int num, int K);

void addressstore(Node ** arr, Node * head) {
    Node * temp = head;
    int i = 0;
    while (temp) {
        arr[i] = temp;
        i++;
        temp = temp - > next;
    }
}

bool check(Node ** before, Node ** after, int num, int K) {
    if (K > num)
        return 1;
return (before[K - 1] == after[num - K]) 
&& (before[num - K] == after[K - 1]);
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int num, K, firstdata;
        cin >> num >> K;
        int temp;
        cin >> firstdata;
        Node * head = new Node(firstdata);
        Node * tail = head;
        for (int i = 0; i < num - 1; i++) {
            cin >> temp;
            tail - > next = new Node(temp);
            tail = tail - > next;
        }

        Node * before[num];
        addressstore(before, head);

        head = swapkthnode(head, num, K);

        Node * after[num];
        addressstore(after, head);

        if (check(before, after, num, K))
            cout << 1 << endl;
        else
            cout << 0 << endl;
    }
}
// } Driver Code Ends


//User function Template for C++

/* Linked List Node structure
   struct Node  {
     int data;
     Node *next;
     Node(int x){
        data = x;
        next = NULL;
         
     }

  }
*/

// Should swap Kth node from beginning and Kth
// node from end in list and return new head.

#include<vector>

Node * swapkthnode(Node * head, int num, int K) {
    if (K > num)
        return head;

    if (num == 0 || head == NULL)
        return NULL;
    if (num == 1)
        return head;
    vector < Node * > v;
    Node * p = head;

    while (p != NULL) {
        Node * temp = p;
        p = p - > next;
        temp - > next = NULL;
        v.push_back(temp);
    }

    swap(v[K - 1], v[num - K]);
    head = v[0];
    int i = 0;

    while (i < num - 1) {
        v[i] - > next = v[i + 1];
        ++i;
    }


    return head;
}