Swap the kth node from both ends in linked list

This is the interesting problem of linked list, swap a node that exists in kth distance from either end. This means swaping a node that are include at k distance from the beginning and k distance from the end. For example.

Example 1
----------
    List    1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
    k = 2
    Swap node
            1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL

                ↑               ↑
    -----------------------------------------
Output :    1 → 6 → 5 → 4 → 3 → 2 → 7 → NULL


Example 2
----------
    List    6 → 3 → 2 → 1 → NULL
    k = 4
    Swap node
            6 → 3 → 2 → 1 → NULL

            ↑           ↑
          4-th node    4-th node
          from end     from start
    -----------------------------------------
Output :    1 → 3 → 2 → 6 → NULL

Example 3
----------
    List    1 → 2 → 3 → 4 → NULL
    k = 3
    Swap node
            1 → 2 → 3 → 4 → NULL
               ⤶    ⤷  
       3-rd node      3-rd node 
       from end       from start
           
    -----------------------------------------
Output :    1 → 3 → 2 → 6 → NULL

We assume given node position are valid. And update the node link, not swap node data value.

Here given code implementation process.

/*
    C program for
    Swap the kth node from both ends in linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
    struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = 
      (struct SingleLL *) malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;

    }
    return sll;
}

// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
    // Create dynamic node
    struct LinkNode *node = 
      (struct LinkNode *) malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow to Create LinkNode\n");
        return;
    }
    else
    {
        // Set initial node value
        node->data = data;
        node->next = NULL;
    }
    if(sll->head == NULL)
    {
        sll->head = node;
  
    }
    else
    {
        sll->tail->next = node;
    }
    sll->tail = node;
   
}
// Display linked list element
void display(struct SingleLL *sll)
{
    if (sll->head == NULL)
    {
        return;
    }
    struct LinkNode *temp = sll->head;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}
// Returns the number of nodes in linked list
int countNode(struct LinkNode *node)
{
    int count = 0;
    while (node != NULL)
    {
        // Visit to next node
        node = node->next;
        count++;
    }
    return count;
}
void swapKthBothSide(struct SingleLL *sll, int k)
{
    if (k <= 0 || sll->head == NULL || sll->head->next == NULL)
    {
        return;
    }
    // Define some auxiliary variables
    struct LinkNode *n1 = NULL;
    struct LinkNode *n2 = NULL;
    struct LinkNode *x = sll->head;
    struct LinkNode *y = sll->head;

    // Count number of nodes
    int count = countNode(sll->head);

    int t = count;
    if (count < k)
    {
        // less than k nodes
        return;
    }
    if ((2 *k) - 1 == count)
    {
        // When middle element are swap in linked list
        // Because both side element is same
        return;
    }
    count = 1;
    // Find first k-1 node
    while (count < k)
    {
        n1 = x;
        // Visit to next node
        x = x->next;
        count++;
    }
    // Reset the value
    count = 1;
 
    // Find last k-1 node
    while (y != NULL && count < t-k+1)
    {
       
        n2 = y;
        // Visit to next node
        y = y->next;
        count++;
    }

    if(n1 != NULL)
    {
        n1->next = y;
    }
    if(n2!=NULL)
    {
        n2->next = x;
    }

  
    struct LinkNode *temp = x->next; 
    x->next = y->next; 
    y->next = temp; 

    if(k==1)
    {
        sll->head = y;
        sll->tail = x;
    }
    if(k==t)
    {
        sll->head = x;
        sll->tail = y;
    }


   
}
// Handles the request of swapping kth nodes in both side
void swapKthNode(struct SingleLL *sll, int k)
{
    if (sll->head == NULL)
    {
        printf("\n Empty Linked List \n");
        return;
    }
    // Display given linked list
    // Before Swap 
    printf("\n Given Linked List  \n");
    display(sll);
    // Swap node
    swapKthBothSide(sll, k);
    // Display resultant linked list
    // After Swap
    printf(" After Swap k = %d node Both side   \n", k);
    display(sll);
}
int main(int argc, char
    const *argv[])
{
    struct SingleLL *sll1 = newLinkedList();
    struct SingleLL *sll2 = newLinkedList();
    struct SingleLL *sll3 = newLinkedList();
    struct SingleLL *sll4 = newLinkedList();
    // First linked list
    //  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
    addNode(sll1, 1);
    addNode(sll1, 2);
    addNode(sll1, 5);
    addNode(sll1, 4);
    addNode(sll1, 3);
    addNode(sll1, 6);
    addNode(sll1, 7);
    // Second linked list
    //  6 → 3 → 2 → 1 → NULL
    addNode(sll2, 6);
    addNode(sll2, 3);
    addNode(sll2, 2);
    addNode(sll2, 1);
    // Third linked list
    //  1 → 2 → 3 → 4 → NULL
    addNode(sll3, 1);
    addNode(sll3, 2);
    addNode(sll3, 3);
    addNode(sll3, 4);
    // Fourth linked list
    //  1 → 2 → NULL
    addNode(sll4, 1);
    addNode(sll4, 2);
    // Test 
    swapKthNode(sll1, 2);
    swapKthNode(sll2, 4);
    swapKthNode(sll3, 3);
    swapKthNode(sll4, 1);
    return 0;
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap k = 2 node Both side
 1 → 6 → 5 → 4 → 3 → 2 → 7 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap k = 4 node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap k = 3 node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap k = 1 node Both side
 2 → 1 → NULL
/*
    Java program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
    public int data;
    public LinkNode next;
    public LinkNode(int data)
    {
        this.data = data;
        this.next = null;
    }
}
public class SingleLL
{
    public LinkNode head;
    public LinkNode tail;
    public SingleLL()
    {
        this.head = null;
        this.tail = null;
    }
    // Add new Node at end of linked list 
    public void addNode(int data)
    {
        LinkNode node = new LinkNode(data);
        if (this.head == null)
        {
            this.head = node;
        }
        else
        {
            // Append the node at last position
            this.tail.next = node;
        }
        this.tail = node;
    }
    // Display linked list element
    public void display()
    {
        if (this.head == null)
        {
            return;
        }
        LinkNode temp = this.head;
        // iterating linked list elements
        while (temp != null)
        {
            System.out.print(" " + temp.data + " →");
            // Visit to next node
            temp = temp.next;
        }
        System.out.print(" NULL\n");
    }
    // Returns the number of nodes in linked list
    public int countNode(LinkNode node)
    {
        int count = 0;
        while (node != null)
        {
            // Visit to next node
            node = node.next;
            count++;
        }
        return count;
    }
    public void swapKthBothSide(int k)
    {
        if (k <= 0 || this.head == null || this.head.next == null)
        {
            return;
        }
        // Define some auxiliary variables
        LinkNode n1 = null;
        LinkNode n2 = null;
        LinkNode x = this.head;
        LinkNode y = this.head;
        // Count number of nodes
        int count = countNode(this.head);
        int t = count;
        if (count < k)
        {
            // less than k nodes
            return;
        }
        if ((2 * k) - 1 == count)
        {
            // When middle element are swap in linked list
            // Because both side element is same
            return;
        }
        count = 1;
        // Find first k-1 node
        while (count < k)
        {
            n1 = x;
            // Visit to next node
            x = x.next;
            count++;
        }
        // Reset the value
        count = 1;
        // Find last k-1 node
        while (y != null && count < t - k + 1)
        {
            n2 = y;
            // Visit to next node
            y = y.next;
            count++;
        }
        if (n1 != null)
        {
            n1.next = y;
        }
        if (n2 != null)
        {
            n2.next = x;
        }
        LinkNode temp = x.next;
        x.next = y.next;
        y.next = temp;
        if (k == 1)
        {
            this.head = y;
            this.tail = x;
        }
        if (k == t)
        {
            this.head = x;
            this.tail = y;
        }
    }
    // Handles the request of swapping kth nodes in both side
    public void swapKthNode( int k)
    {
        if (this.head == null)
        {
            System.out.print("\n Empty Linked List \n");
            return;
        }
        // Display given linked list
        // Before Swap 
        System.out.print("\n Given Linked List \n");
        this.display();
        // Swap node
        this.swapKthBothSide(k);
        // Display resultant linked list
        // After Swap
        System.out.print(" After Swap " + k + "-th node Both side \n");
        this.display();
    }
    public static void main(String[] args)
    {
        SingleLL sll1 = new SingleLL();
        SingleLL sll2 = new SingleLL();
        SingleLL sll3 = new SingleLL();
        SingleLL sll4 = new SingleLL();
        // First linked list
        //  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
        sll1.addNode(1);
        sll1.addNode(2);
        sll1.addNode(5);
        sll1.addNode(4);
        sll1.addNode(3);
        sll1.addNode(6);
        sll1.addNode(7);
        // Second linked list
        //  6 → 3 → 2 → 1 → NULL
        sll2.addNode(6);
        sll2.addNode(3);
        sll2.addNode(2);
        sll2.addNode(1);
        // Third linked list
        //  1 → 2 → 3 → 4 → NULL
        sll3.addNode(1);
        sll3.addNode(2);
        sll3.addNode(3);
        sll3.addNode(4);
        // Fourth linked list
        //  1 → 2 → NULL
        sll4.addNode(1);
        sll4.addNode(2);
        // Test 
        sll1.swapKthNode(1);
        sll2.swapKthNode(4);
        sll3.swapKthNode(3);
        sll4.swapKthNode(1);
    }
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
	public: 
    int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	// Add new Node at end of linked list 
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			return;
		}
		LinkNode *temp = this->head;
		// iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// Returns the number of nodes in linked list
	int countNode(LinkNode *node)
	{
		int count = 0;
		while (node != NULL)
		{
			// Visit to next node
			node = node->next;
			count++;
		}
		return count;
	}
	void swapKthBothSide(int k)
	{
		if (k <= 0 || this->head == NULL || this->head->next == NULL)
		{
			return;
		}
		// Define some auxiliary variables
		LinkNode *n1 = NULL;
		LinkNode *n2 = NULL;
		LinkNode *x = this->head;
		LinkNode *y = this->head;
		// Count number of nodes
		int count = this->countNode(this->head);
		int t = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 *k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x->next;
			count++;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y != NULL && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y->next;
			count++;
		}
		if (n1 != NULL)
		{
			n1->next = y;
		}
		if (n2 != NULL)
		{
			n2->next = x;
		}
		LinkNode *temp = x->next;
		x->next = y->next;
		y->next = temp;
		if (k == 1)
		{
			this->head = y;
			this->tail = x;
		}
		if (k == t)
		{
			this->head = x;
			this->tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	void swapKthNode(int k)
	{
		if (this->head == NULL)
		{
			cout << "\n Empty Linked List \n";
			return;
		}
		// Display given linked list
		// Before Swap 
		cout << "\n Given Linked List \n";
		this->display();
		// Swap node
		this->swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		cout << " After Swap " << k << "-th node Both side \n";
		this->display();
	}
};
int main()
{
	SingleLL *sll1 = new SingleLL();
	SingleLL *sll2 = new SingleLL();
	SingleLL *sll3 = new SingleLL();
	SingleLL *sll4 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1->addNode(1);
	sll1->addNode(2);
	sll1->addNode(5);
	sll1->addNode(4);
	sll1->addNode(3);
	sll1->addNode(6);
	sll1->addNode(7);
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	sll2->addNode(6);
	sll2->addNode(3);
	sll2->addNode(2);
	sll2->addNode(1);
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	sll3->addNode(1);
	sll3->addNode(2);
	sll3->addNode(3);
	sll3->addNode(4);
	// Fourth linked list
	//  1 → 2 → NULL
	sll4->addNode(1);
	sll4->addNode(2);
	// Test 
	sll1->swapKthNode(1);
	sll2->swapKthNode(4);
	sll3->swapKthNode(3);
	sll4->swapKthNode(1);
	return 0;
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
// Include namespace system
using System;
/*
    Csharp program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			return;
		}
		LinkNode temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" NULL\n");
	}
	// Returns the number of nodes in linked list
	public int countNode(LinkNode node)
	{
		int count = 0;
		while (node != null)
		{
			// Visit to next node
			node = node.next;
			count++;
		}
		return count;
	}
	public void swapKthBothSide(int k)
	{
		if (k <= 0 || this.head == null || this.head.next == null)
		{
			return;
		}
		// Define some auxiliary variables
		LinkNode n1 = null;
		LinkNode n2 = null;
		LinkNode x = this.head;
		LinkNode y = this.head;
		// Count number of nodes
		int count = this.countNode(this.head);
		int t = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 * k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x.next;
			count++;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y != null && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y.next;
			count++;
		}
		if (n1 != null)
		{
			n1.next = y;
		}
		if (n2 != null)
		{
			n2.next = x;
		}
		LinkNode temp = x.next;
		x.next = y.next;
		y.next = temp;
		if (k == 1)
		{
			this.head = y;
			this.tail = x;
		}
		if (k == t)
		{
			this.head = x;
			this.tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	public void swapKthNode(int k)
	{
		if (this.head == null)
		{
			Console.Write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Swap 
		Console.Write("\n Given Linked List \n");
		this.display();
		// Swap node
		this.swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		Console.Write(" After Swap " + k + "-th node Both side \n");
		this.display();
	}
	public static void Main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		SingleLL sll3 = new SingleLL();
		SingleLL sll4 = new SingleLL();
		// First linked list
		//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(6);
		sll1.addNode(7);
		// Second linked list
		//  6 → 3 → 2 → 1 → NULL
		sll2.addNode(6);
		sll2.addNode(3);
		sll2.addNode(2);
		sll2.addNode(1);
		// Third linked list
		//  1 → 2 → 3 → 4 → NULL
		sll3.addNode(1);
		sll3.addNode(2);
		sll3.addNode(3);
		sll3.addNode(4);
		// Fourth linked list
		//  1 → 2 → NULL
		sll4.addNode(1);
		sll4.addNode(2);
		// Test 
		sll1.swapKthNode(1);
		sll2.swapKthNode(4);
		sll3.swapKthNode(3);
		sll4.swapKthNode(1);
	}
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
package main
import "fmt"
/*
    Go program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int) * LinkNode {
	var me *LinkNode = &LinkNode {}
	me.data = data
	me.next = nil
	return me
}
type SingleLL struct {
	head * LinkNode
	tail * LinkNode
}
func getSingleLL() * SingleLL {
	var me *SingleLL = &SingleLL {}
	me.head = nil
	me.tail = nil
	return me
}
// Add new Node at end of linked list 
func(this *SingleLL) addNode(data int) {
	var node * LinkNode = getLinkNode(data)
	if this.head == nil {
		this.head = node
	} else {
		// Append the node at last position
		this.tail.next = node
	}
	this.tail = node
}
// Display linked list element
func(this SingleLL) display() {
	if this.head == nil {
		return
	}
	var temp * LinkNode = this.head
	// iterating linked list elements
	for (temp != nil) {
		fmt.Print(" ", temp.data, " →")
		// Visit to next node
		temp = temp.next
	}
	fmt.Print(" NULL\n")
}
// Returns the number of nodes in linked list
func(this SingleLL) countNode(node * LinkNode) int {
	var count int = 0
	for (node != nil) {
		// Visit to next node
		node = node.next
		count++
	}
	return count
}
func(this *SingleLL) swapKthBothSide(k int) {
	if k <= 0 || this.head == nil || this.head.next == nil {
		return
	}
	// Define some auxiliary variables
	var n1 * LinkNode = nil
	var n2 * LinkNode = nil
	var x * LinkNode = this.head
	var y * LinkNode = this.head
	// Count number of nodes
	var count int = this.countNode(this.head)
	var t int = count
	if count < k {
		// less than k nodes
		return
	}
	if (2 * k) - 1 == count {
		// When middle element are swap in linked list
		// Because both side element is same
		return
	}
	count = 1
	// Find first k-1 node
	for (count < k) {
		n1 = x
		// Visit to next node
		x = x.next
		count++
	}
	// Reset the value
	count = 1
	// Find last k-1 node
	for (y != nil && count < t - k + 1) {
		n2 = y
		// Visit to next node
		y = y.next
		count++
	}
	if n1 != nil {
		n1.next = y
	}
	if n2 != nil {
		n2.next = x
	}
	var temp * LinkNode = x.next
	x.next = y.next
	y.next = temp
	if k == 1 {
		this.head = y
		this.tail = x
	}
	if k == t {
		this.head = x
		this.tail = y
	}
}
// Handles the request of swapping kth nodes in both side
func(this *SingleLL) swapKthNode(k int) {
	if this.head == nil {
		fmt.Print("\n Empty Linked List \n")
		return
	}
	// Display given linked list
	// Before Swap 
	fmt.Print("\n Given Linked List \n")
	this.display()
	// Swap node
	this.swapKthBothSide(k)
	// Display resultant linked list
	// After Swap
	fmt.Print(" After Swap ", k, "-th node Both side \n")
	this.display()
}
func main() {
	var sll1 * SingleLL = getSingleLL()
	var sll2 * SingleLL = getSingleLL()
	var sll3 * SingleLL = getSingleLL()
	var sll4 * SingleLL = getSingleLL()
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	sll2.addNode(1)
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	sll3.addNode(4)
	// Fourth linked list
	//  1 → 2 → NULL
	sll4.addNode(1)
	sll4.addNode(2)
	// Test 
	sll1.swapKthNode(1)
	sll2.swapKthNode(4)
	sll3.swapKthNode(3)
	sll4.swapKthNode(1)
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
<?php
/*
    Php program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class SingleLL
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	// Add new Node at end of linked list 
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo(" ".$temp->data.
				" →");
			// Visit to next node
			$temp = $temp->next;
		}
		echo(" NULL\n");
	}
	// Returns the number of nodes in linked list
	public	function countNode($node)
	{
		$count = 0;
		while ($node != NULL)
		{
			// Visit to next node
			$node = $node->next;
			$count++;
		}
		return $count;
	}
	public	function swapKthBothSide($k)
	{
		if ($k <= 0 || $this->head == NULL || $this->head->next == NULL)
		{
			return;
		}
		// Define some auxiliary variables
		$n1 = NULL;
		$n2 = NULL;
		$x = $this->head;
		$y = $this->head;
		// Count number of nodes
		$count = $this->countNode($this->head);
		$t = $count;
		if ($count < $k)
		{
			// less than k nodes
			return;
		}
		if ((2 * $k) - 1 == $count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		$count = 1;
		// Find first k-1 node
		while ($count < $k)
		{
			$n1 = $x;
			// Visit to next node
			$x = $x->next;
			$count++;
		}
		// Reset the value
		$count = 1;
		// Find last k-1 node
		while ($y != NULL && $count < $t - $k + 1)
		{
			$n2 = $y;
			// Visit to next node
			$y = $y->next;
			$count++;
		}
		if ($n1 != NULL)
		{
			$n1->next = $y;
		}
		if ($n2 != NULL)
		{
			$n2->next = $x;
		}
		$temp = $x->next;
		$x->next = $y->next;
		$y->next = $temp;
		if ($k == 1)
		{
			$this->head = $y;
			$this->tail = $x;
		}
		if ($k == $t)
		{
			$this->head = $x;
			$this->tail = $y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	public	function swapKthNode($k)
	{
		if ($this->head == NULL)
		{
			echo("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Swap 
		echo("\n Given Linked List \n");
		$this->display();
		// Swap node
		$this->swapKthBothSide($k);
		// Display resultant linked list
		// After Swap
		echo(" After Swap ".$k.
			"-th node Both side \n");
		$this->display();
	}
}

function main()
{
	$sll1 = new SingleLL();
	$sll2 = new SingleLL();
	$sll3 = new SingleLL();
	$sll4 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	$sll1->addNode(1);
	$sll1->addNode(2);
	$sll1->addNode(5);
	$sll1->addNode(4);
	$sll1->addNode(3);
	$sll1->addNode(6);
	$sll1->addNode(7);
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	$sll2->addNode(6);
	$sll2->addNode(3);
	$sll2->addNode(2);
	$sll2->addNode(1);
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	$sll3->addNode(1);
	$sll3->addNode(2);
	$sll3->addNode(3);
	$sll3->addNode(4);
	// Fourth linked list
	//  1 → 2 → NULL
	$sll4->addNode(1);
	$sll4->addNode(2);
	// Test 
	$sll1->swapKthNode(1);
	$sll2->swapKthNode(4);
	$sll3->swapKthNode(3);
	$sll4->swapKthNode(1);
}
main();

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
/*
    Node JS program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			return;
		}
		var temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	// Returns the number of nodes in linked list
	countNode(node)
	{
		var count = 0;
		while (node != null)
		{
			// Visit to next node
			node = node.next;
			count++;
		}
		return count;
	}
	swapKthBothSide(k)
	{
		if (k <= 0 || this.head == null || this.head.next == null)
		{
			return;
		}
		// Define some auxiliary variables
		var n1 = null;
		var n2 = null;
		var x = this.head;
		var y = this.head;
		// Count number of nodes
		var count = this.countNode(this.head);
		var t = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 * k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x.next;
			count++;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y != null && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y.next;
			count++;
		}
		if (n1 != null)
		{
			n1.next = y;
		}
		if (n2 != null)
		{
			n2.next = x;
		}
		var temp = x.next;
		x.next = y.next;
		y.next = temp;
		if (k == 1)
		{
			this.head = y;
			this.tail = x;
		}
		if (k == t)
		{
			this.head = x;
			this.tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	swapKthNode(k)
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Swap 
		process.stdout.write("\n Given Linked List \n");
		this.display();
		// Swap node
		this.swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		process.stdout.write(" After Swap " + k + "-th node Both side \n");
		this.display();
	}
}

function main()
{
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	var sll3 = new SingleLL();
	var sll4 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	sll2.addNode(1);
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	sll3.addNode(4);
	// Fourth linked list
	//  1 → 2 → NULL
	sll4.addNode(1);
	sll4.addNode(2);
	// Test 
	sll1.swapKthNode(1);
	sll2.swapKthNode(4);
	sll3.swapKthNode(3);
	sll4.swapKthNode(1);
}
main();

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
#    Python 3 program for
#    Swap the kth node from both ends in linked list

#  Linked list node
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			#  Append the node at last position
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			return
		
		temp = self.head
		#  iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  Returns the number of nodes in linked list
	def countNode(self, node) :
		count = 0
		while (node != None) :
			#  Visit to next node
			node = node.next
			count += 1
		
		return count
	
	def swapKthBothSide(self, k) :
		if (k <= 0 or self.head == None or self.head.next == None) :
			return
		
		#  Define some auxiliary variables
		n1 = None
		n2 = None
		x = self.head
		y = self.head
		#  Count number of nodes
		count = self.countNode(self.head)
		t = count
		if (count < k) :
			#  less than k nodes
			return
		
		if ((2 * k) - 1 == count) :
			#  When middle element are swap in linked list
			#  Because both side element is same
			return
		
		count = 1
		#  Find first k-1 node
		while (count < k) :
			n1 = x
			#  Visit to next node
			x = x.next
			count += 1
		
		#  Reset the value
		count = 1
		#  Find last k-1 node
		while (y != None and count < t - k + 1) :
			n2 = y
			#  Visit to next node
			y = y.next
			count += 1
		
		if (n1 != None) :
			n1.next = y
		
		if (n2 != None) :
			n2.next = x
		
		temp = x.next
		x.next = y.next
		y.next = temp
		if (k == 1) :
			self.head = y
			self.tail = x
		
		if (k == t) :
			self.head = x
			self.tail = y
		
	
	#  Handles the request of swapping kth nodes in both side
	def swapKthNode(self, k) :
		if (self.head == None) :
			print("\n Empty Linked List ")
			return
		
		#  Display given linked list
		#  Before Swap 
		print("\n Given Linked List ")
		self.display()
		#  Swap node
		self.swapKthBothSide(k)
		#  Display resultant linked list
		#  After Swap
		print(" After Swap ", k ,"-th node Both side ")
		self.display()
	

def main() :
	sll1 = SingleLL()
	sll2 = SingleLL()
	sll3 = SingleLL()
	sll4 = SingleLL()
	#  First linked list
	#   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	#  Second linked list
	#   6 → 3 → 2 → 1 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	sll2.addNode(1)
	#  Third linked list
	#   1 → 2 → 3 → 4 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	sll3.addNode(4)
	#  Fourth linked list
	#   1 → 2 → NULL
	sll4.addNode(1)
	sll4.addNode(2)
	#  Test 
	sll1.swapKthNode(1)
	sll2.swapKthNode(4)
	sll3.swapKthNode(3)
	sll4.swapKthNode(1)

if __name__ == "__main__": main()

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap  1 -th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap  4 -th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap  3 -th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap  1 -th node Both side
 2 → 1 → NULL
#    Ruby program for
#    Swap the kth node from both ends in linked list

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL 
	# Define the accessor and reader of class SingleLL
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	#  Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else
 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			return
		end

		temp = self.head
		#  iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL\n")
	end

	#  Returns the number of nodes in linked list
	def countNode(node) 
		count = 0
		while (node != nil) 
			#  Visit to next node
			node = node.next
			count += 1
		end

		return count
	end

	def swapKthBothSide(k) 
		if (k <= 0 || self.head == nil || self.head.next == nil) 
			return
		end

		#  Define some auxiliary variables
		n1 = nil
		n2 = nil
		x = self.head
		y = self.head
		#  Count number of nodes
		count = self.countNode(self.head)
		t = count
		if (count < k) 
			#  less than k nodes
			return
		end

		if ((2 * k) - 1 == count) 
			#  When middle element are swap in linked list
			#  Because both side element is same
			return
		end

		count = 1
		#  Find first k-1 node
		while (count < k) 
			n1 = x
			#  Visit to next node
			x = x.next
			count += 1
		end

		#  Reset the value
		count = 1
		#  Find last k-1 node
		while (y != nil && count < t - k + 1) 
			n2 = y
			#  Visit to next node
			y = y.next
			count += 1
		end

		if (n1 != nil) 
			n1.next = y
		end

		if (n2 != nil) 
			n2.next = x
		end

		temp = x.next
		x.next = y.next
		y.next = temp
		if (k == 1) 
			self.head = y
			self.tail = x
		end

		if (k == t) 
			self.head = x
			self.tail = y
		end

	end

	#  Handles the request of swapping kth nodes in both side
	def swapKthNode(k) 
		if (self.head == nil) 
			print("\n Empty Linked List \n")
			return
		end

		#  Display given linked list
		#  Before Swap 
		print("\n Given Linked List \n")
		self.display()
		#  Swap node
		self.swapKthBothSide(k)
		#  Display resultant linked list
		#  After Swap
		print(" After Swap ", k ,"-th node Both side \n")
		self.display()
	end

end

def main() 
	sll1 = SingleLL.new()
	sll2 = SingleLL.new()
	sll3 = SingleLL.new()
	sll4 = SingleLL.new()
	#  First linked list
	#   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	#  Second linked list
	#   6 → 3 → 2 → 1 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	sll2.addNode(1)
	#  Third linked list
	#   1 → 2 → 3 → 4 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	sll3.addNode(4)
	#  Fourth linked list
	#   1 → 2 → NULL
	sll4.addNode(1)
	sll4.addNode(2)
	#  Test 
	sll1.swapKthNode(1)
	sll2.swapKthNode(4)
	sll3.swapKthNode(3)
	sll4.swapKthNode(1)
end

main()

Output

 Given Linked List 
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side 
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List 
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side 
 1 → 3 → 2 → 6 → NULL

 Given Linked List 
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side 
 1 → 3 → 2 → 4 → NULL

 Given Linked List 
 1 → 2 → NULL
 After Swap 1-th node Both side 
 2 → 1 → NULL
/*
    Scala program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode(var data: Int,
	var next: LinkNode)
{
	def this(data: Int)
	{
		this(data,null);
	}
}
class SingleLL(var head: LinkNode,
	var tail: LinkNode)
{
	def this()
	{
		this(null, null);
	}
	// Add new Node at end of linked list 
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			return;
		}
		var temp: LinkNode = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Returns the number of nodes in linked list
	def countNode(): Int = {
		var count: Int = 0;
      	var node = this.head;
		while (node != null)
		{
			// Visit to next node
			node = node.next;
			count += 1;
		}
		return count;
	}
	def swapKthBothSide(k: Int): Unit = {
		if (k <= 0 || this.head == null || this.head.next == null)
		{
			return;
		}
		// Define some auxiliary variables
		var n1: LinkNode = null;
		var n2: LinkNode = null;
		var x: LinkNode = this.head;
		var y: LinkNode = this.head;
		// Count number of nodes
		var count: Int = countNode();
		var t: Int = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 * k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x.next;
			count += 1;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y != null && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y.next;
			count += 1;
		}
		if (n1 != null)
		{
			n1.next = y;
		}
		if (n2 != null)
		{
			n2.next = x;
		}
		var temp: LinkNode = x.next;
		x.next = y.next;
		y.next = temp;
		if (k == 1)
		{
			this.head = y;
			this.tail = x;
		}
		if (k == t)
		{
			this.head = x;
			this.tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	def swapKthNode(k: Int): Unit = {
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Swap 
		print("\n Given Linked List \n");
		this.display();
		// Swap node
		this.swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		print(" After Swap " + k + "-th node Both side \n");
		this.display();
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll1: SingleLL = new SingleLL();
		var sll2: SingleLL = new SingleLL();
		var sll3: SingleLL = new SingleLL();
		var sll4: SingleLL = new SingleLL();
		// First linked list
		//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(6);
		sll1.addNode(7);
		// Second linked list
		//  6 → 3 → 2 → 1 → NULL
		sll2.addNode(6);
		sll2.addNode(3);
		sll2.addNode(2);
		sll2.addNode(1);
		// Third linked list
		//  1 → 2 → 3 → 4 → NULL
		sll3.addNode(1);
		sll3.addNode(2);
		sll3.addNode(3);
		sll3.addNode(4);
		// Fourth linked list
		//  1 → 2 → NULL
		sll4.addNode(1);
		sll4.addNode(2);
		// Test 
		sll1.swapKthNode(1);
		sll2.swapKthNode(4);
		sll3.swapKthNode(3);
		sll4.swapKthNode(1);
	}
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL
/*
    Swift 4 program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list 
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp?.next;
		}
		print(" NULL");
	}
	// Returns the number of nodes in linked list
	func countNode() -> Int
	{
		var count: Int = 0;
		var node: LinkNode? = self.head;
		while (node  != nil)
		{
			// Visit to next node
			node = node!.next;
			count += 1;
		}
		return count;
	}
	func swapKthBothSide(_ k: Int)
	{
		if (k <= 0 || self.head == nil || self.head!.next == nil)
		{
			return;
		}
		// Define some auxiliary variables
		var n1: LinkNode? = nil;
		var n2: LinkNode? = nil;
		var x: LinkNode? = self.head;
		var y: LinkNode? = self.head;
		// Count number of nodes
		var count: Int = self.countNode();
		let t: Int = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 * k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x!.next;
			count += 1;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y  != nil && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y!.next;
			count += 1;
		}
		if (n1  != nil)
		{
			n1!.next = y;
		}
		if (n2  != nil)
		{
			n2!.next = x;
		}
		let temp: LinkNode? = x!.next;
		x!.next = y!.next;
		y!.next = temp;
		if (k == 1)
		{
			self.head = y;
			self.tail = x;
		}
		if (k == t)
		{
			self.head = x;
			self.tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	func swapKthNode(_ k: Int)
	{
		if (self.head == nil)
		{
			print("\n Empty Linked List ");
			return;
		}
		// Display given linked list
		// Before Swap 
		print("\n Given Linked List ");
		self.display();
		// Swap node
		self.swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		print(" After Swap ", k ,"-th node Both side ");
		self.display();
	}
}
func main()
{
	let sll1: SingleLL = SingleLL();
	let sll2: SingleLL = SingleLL();
	let sll3: SingleLL = SingleLL();
	let sll4: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	sll2.addNode(1);
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	sll3.addNode(4);
	// Fourth linked list
	//  1 → 2 → NULL
	sll4.addNode(1);
	sll4.addNode(2);
	// Test 
	sll1.swapKthNode(1);
	sll2.swapKthNode(4);
	sll3.swapKthNode(3);
	sll4.swapKthNode(1);
}
main();

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap  1 -th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap  4 -th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap  3 -th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap  1 -th node Both side
 2 → 1 → NULL
/*
    Kotlin program for
    Swap the kth node from both ends in linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	fun addNode(data: Int): Unit
	{
		var node: LinkNode ? = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			return;
		}
		var temp: LinkNode? = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// Returns the number of nodes in linked list
	fun countNode(): Int
	{
		var count: Int = 0;
		var node: LinkNode? = this.head;
		while (node != null)
		{
			// Visit to next node
			node = node.next;
			count += 1;
		}
		return count;
	}
	fun swapKthBothSide(k: Int): Unit
	{
		if (k <= 0 || this.head == null || this.head?.next == null)
		{
			return;
		}
		// Define some auxiliary variables
		var n1: LinkNode ? = null;
		var n2: LinkNode ? = null;
		var x: LinkNode ? = this.head;
		var y: LinkNode ? = this.head;
		// Count number of nodes
		var count: Int = this.countNode();
		val t: Int = count;
		if (count < k)
		{
			// less than k nodes
			return;
		}
		if ((2 * k) - 1 == count)
		{
			// When middle element are swap in linked list
			// Because both side element is same
			return;
		}
		count = 1;
		// Find first k-1 node
		while (count < k)
		{
			n1 = x;
			// Visit to next node
			x = x?.next;
			count += 1;
		}
		// Reset the value
		count = 1;
		// Find last k-1 node
		while (y != null && count < t - k + 1)
		{
			n2 = y;
			// Visit to next node
			y = y.next;
			count += 1;
		}
		if (n1 != null)
		{
			n1.next = y;
		}
		if (n2 != null)
		{
			n2.next = x;
		}
		val temp: LinkNode? = x?.next;
		x?.next = y?.next;
		y?.next = temp;
		if (k == 1)
		{
			this.head = y;
			this.tail = x;
		}
		if (k == t)
		{
			this.head = x;
			this.tail = y;
		}
	}
	// Handles the request of swapping kth nodes in both side
	fun swapKthNode(k: Int): Unit
	{
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Swap 
		print("\n Given Linked List \n");
		this.display();
		// Swap node
		this.swapKthBothSide(k);
		// Display resultant linked list
		// After Swap
		print(" After Swap " + k + "-th node Both side \n");
		this.display();
	}
}
fun main(args: Array < String > ): Unit
{
	val sll1: SingleLL = SingleLL();
	val sll2: SingleLL = SingleLL();
	val sll3: SingleLL = SingleLL();
	val sll4: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	// Second linked list
	//  6 → 3 → 2 → 1 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	sll2.addNode(1);
	// Third linked list
	//  1 → 2 → 3 → 4 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	sll3.addNode(4);
	// Fourth linked list
	//  1 → 2 → NULL
	sll4.addNode(1);
	sll4.addNode(2);
	// Test 
	sll1.swapKthNode(1);
	sll2.swapKthNode(4);
	sll3.swapKthNode(3);
	sll4.swapKthNode(1);
}

Output

 Given Linked List
 1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
 After Swap 1-th node Both side
 7 → 2 → 5 → 4 → 3 → 6 → 1 → NULL

 Given Linked List
 6 → 3 → 2 → 1 → NULL
 After Swap 4-th node Both side
 1 → 3 → 2 → 6 → NULL

 Given Linked List
 1 → 2 → 3 → 4 → NULL
 After Swap 3-th node Both side
 1 → 3 → 2 → 4 → NULL

 Given Linked List
 1 → 2 → NULL
 After Swap 1-th node Both side
 2 → 1 → NULL

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved