# 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>

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

}
return sll;
}

void addNode(struct SingleLL *sll, int data)
{
// Create dynamic node
if (node == NULL)
{
return;
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
{

}
else
{
sll->tail->next = node;
}
sll->tail = node;

}
void display(struct SingleLL *sll)
{
{
return;
}
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 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

// Count number of nodes

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;
}

x->next = y->next;
y->next = temp;

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

}
// Handles the request of swapping kth nodes in both side
void swapKthNode(struct SingleLL *sll, int k)
{
{
return;
}
// Before Swap
display(sll);
// Swap node
swapKthBothSide(sll, k);
// After Swap
printf(" After Swap k = %d node Both side   \n", k);
display(sll);
}
int main(int argc, char
const *argv[])
{
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
public void display()
{
{
return;
}
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
{
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
// Count number of nodes
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;
}
x.next = y.next;
y.next = temp;
if (k == 1)
{
this.tail = x;
}
if (k == t)
{
this.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
public void swapKthNode( int k)
{
{
return;
}
// Before Swap
this.display();
// Swap node
this.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
public:
int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
SingleLL()
{
this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
void display()
{
{
return;
}
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 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
// Count number of nodes
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;
}
x->next = y->next;
y->next = temp;
if (k == 1)
{
this->tail = x;
}
if (k == t)
{
this->tail = y;
}
}
// Handles the request of swapping kth nodes in both side
void swapKthNode(int k)
{
{
cout << "\n Empty Linked List \n";
return;
}
// Before Swap
cout << "\n Given Linked List \n";
this->display();
// Swap node
this->swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
public void display()
{
{
return;
}
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
{
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
// Count number of nodes
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;
}
x.next = y.next;
y.next = temp;
if (k == 1)
{
this.tail = x;
}
if (k == t)
{
this.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
public void swapKthNode(int k)
{
{
return;
}
// Before Swap
this.display();
// Swap node
this.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
data int
}
me.data = data
me.next = nil
return me
}
type SingleLL struct {
}
func getSingleLL() * SingleLL {
var me *SingleLL = &SingleLL {}
me.tail = nil
return me
}
} else {
// Append the node at last position
this.tail.next = node
}
this.tail = node
}
func(this SingleLL) display() {
return
}
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
// Count number of nodes
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.tail = x
}
if k == t {
this.tail = y
}
}
// Handles the request of swapping kth nodes in both side
func(this *SingleLL) swapKthNode(k int) {
return
}
// Before Swap
this.display()
// Swap node
this.swapKthBothSide(k)
// 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()
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
public \$data;
public \$next;
public	function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
class SingleLL
{
public \$tail;
public	function __construct()
{
\$this->tail = NULL;
}
{
{
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
public	function display()
{
{
return;
}
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;
// Count number of nodes
\$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->tail = \$x;
}
if (\$k == \$t)
{
\$this->tail = \$y;
}
}
// Handles the request of swapping kth nodes in both side
public	function swapKthNode(\$k)
{
{
return;
}
// Before Swap
\$this->display();
// Swap node
\$this->swapKthBothSide(\$k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
display()
{
{
return;
}
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;
// Count number of nodes
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.tail = x;
}
if (k == t)
{
this.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
swapKthNode(k)
{
{
return;
}
// Before Swap
this.display();
// Swap node
this.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :
def __init__(self) :
self.tail = None

else :
#  Append the node at last position
self.tail.next = node

self.tail = node

def display(self) :
return

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
#  Count number of nodes
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.tail = x

if (k == t) :
self.tail = y

#  Handles the request of swapping kth nodes in both side
def swapKthNode(self, k) :
return

#  Before Swap
self.display()
#  Swap node
self.swapKthBothSide(k)
#  After Swap
print(" After Swap ", k ,"-th node Both side ")
self.display()

def main() :
sll1 = SingleLL()
sll2 = SingleLL()
sll3 = SingleLL()
sll4 = SingleLL()
#   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
#   6 → 3 → 2 → 1 → NULL
#   1 → 2 → 3 → 4 → NULL
#   1 → 2 → NULL
#  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

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

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

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

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
def initialize()
self.tail = nil
end

else

#  Append the node at last position
self.tail.next = node
end

self.tail = node
end

def display()
return
end

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
#  Count number of nodes
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.tail = x
end

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

end

#  Handles the request of swapping kth nodes in both side
def swapKthNode(k)
return
end

#  Before Swap
self.display()
#  Swap node
self.swapKthBothSide(k)
#  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()
#   1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
#   6 → 3 → 2 → 1 → NULL
#   1 → 2 → 3 → 4 → NULL
#   1 → 2 → NULL
#  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

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

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

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
*/
{
def this(data: Int)
{
this(data,null);
}
}
{
def this()
{
this(null, null);
}
def addNode(data: Int): Unit = {
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
def display(): Unit = {
{
return;
}
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;
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
// 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;
}
x.next = y.next;
y.next = temp;
if (k == 1)
{
this.tail = x;
}
if (k == t)
{
this.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
def swapKthNode(k: Int): Unit = {
{
return;
}
// Before Swap
this.display();
// Swap node
this.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
init()
{
self.tail = nil;
}
{
{
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
func display()
{
{
return;
}
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;
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
// 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;
}
x!.next = y!.next;
y!.next = temp;
if (k == 1)
{
self.tail = x;
}
if (k == t)
{
self.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
func swapKthNode(_ k: Int)
{
{
return;
}
// Before Swap
self.display();
// Swap node
self.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
{
{
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
fun display(): Unit
{
{
return;
}
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;
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;
// 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;
}
x?.next = y?.next;
y?.next = temp;
if (k == 1)
{
this.tail = x;
}
if (k == t)
{
this.tail = y;
}
}
// Handles the request of swapping kth nodes in both side
fun swapKthNode(k: Int): Unit
{
{
return;
}
// Before Swap
this.display();
// Swap node
this.swapKthBothSide(k);
// 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();
//  1 → 2 → 5 → 4 → 3 → 6 → 7 → NULL
//  6 → 3 → 2 → 1 → NULL
//  1 → 2 → 3 → 4 → NULL
//  1 → 2 → NULL
// 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

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

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

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.