Games  

Swap Nodes in Pairs Geeksforgeeks

Pairwise bandy elements of a given linked listing

Given a singly linked list, write a function to bandy elements pairwise.

Input : one->2->3->4->5->6->NULL
Output : 2->ane->4->3->6->five->NULL

Input : 1->two->three->4->5->NULL
Output : 2->1->iv->3->five->NULL

Input : 1->NULL
Output : i->Zip

For example, if the linked list is 1->two->three->4->5 then the function should change it to ii->i->4->3->five, and if the linked list is and then the office should alter it to.

METHOD i (Iterative)

Showtime from the head node and traverse the listing. While traversing bandy information of each node with its adjacent node’s data.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>

using
namespace
std;

form
Node {

public
:


int
data;


Node* next;

};

void
pairWiseSwap(Node* head)

{


Node* temp = head;


while
(temp != Nix && temp->side by side != Null) {


bandy(temp->data,


temp->side by side->information);


temp = temp->side by side->adjacent;


}

}

void
push button(Node** head_ref,

int
new_data)

{


Node* new_node =

new
Node();


new_node->data = new_data;


new_node->next = (*head_ref);


(*head_ref) = new_node;

}

void
printList(Node* node)

{


while
(node != Zilch) {


cout << node->data <<

" "
;


node = node->next;


}

}

int
principal()

{


Node* start = Zero;


push(&start, five);


push button(&start, 4);


push(&offset, iii);


push(&offset, 2);


push(&start, one);


cout <<

"Linked list "


<<

"before calling pairWiseSwap()\n"
;


printList(commencement);


pairWiseSwap(start);


cout <<

"\nLinked list "


<<

"after calling pairWiseSwap()\n"
;


printList(start);


render
0;

}

C

#include <stdio.h>

#include <stdlib.h>

struct
Node {


int
data;


struct
Node* adjacent;

};

void
swap(
int
* a,

int
* b);

void
pairWiseSwap(
struct
Node* head)

{


struct
Node* temp = head;


while
(temp != Nothing && temp->next != NULL) {


swap(&temp->information, &temp->side by side->data);


temp = temp->next->adjacent;


}

}

void
swap(
int
* a,

int
* b)

{


int
temp;


temp = *a;


*a = *b;


*b = temp;

}

void
push(
struct
Node** head_ref,

int
new_data)

{


struct
Node* new_node = (
struct
Node*)
malloc
(
sizeof
(
struct
Node));


new_node->data = new_data;


new_node->next = (*head_ref);


(*head_ref) = new_node;

}

void
printList(
struct
Node* node)

{


while
(node != Zippo) {


printf
(
"%d "
, node->information);


node = node->next;


}

}

int
master()

{


struct
Node* start = Cypher;


button(&start, five);


button(&outset, 4);


push(&start, three);


push(&start, 2);


push(&outset, i);


printf
(
"Linked list earlier calling pairWiseSwap()\n"
);


printList(beginning);


pairWiseSwap(first);


printf
(
"\nLinked listing after calling pairWiseSwap()\due north"
);


printList(start);


return
0;

}

Java

class
LinkedList {


Node caput;


class
Node {


int
data;


Node adjacent;


Node(
int
d)


{


data = d;


next =

null
;


}


}


void
pairWiseSwap()


{


Node temp = caput;


while
(temp !=

nothing
&& temp.next !=

cypher
) {


int
yard = temp.data;


temp.information = temp.side by side.data;


temp.adjacent.data = grand;


temp = temp.adjacent.side by side;


}


}


public
void
push(
int
new_data)


{


Node new_node =

new
Node(new_data);


new_node.next = head;


head = new_node;


}


void
printList()


{


Node temp = caput;


while
(temp !=

null
) {


System.out.impress(temp.information +

" "
);


temp = temp.side by side;


}


System.out.println();


}


public
static
void
main(String args[])


{


LinkedList llist =

new
LinkedList();


llist.push button(
5
);


llist.push(
4
);


llist.push(
3
);


llist.push(
ii
);


llist.push(
1
);


Arrangement.out.println(
"Linked Listing earlier calling pairWiseSwap() "
);


llist.printList();


llist.pairWiseSwap();


System.out.println(
"Linked List subsequently calling pairWiseSwap() "
);


llist.printList();


}

}

Python

form
Node:


def
__init__(
cocky
, data):


cocky
.information

=
information


self
.
next
=
None

class
LinkedList:


def
__init__(
self
):


self
.caput

=
None


def
pairwiseSwap(
self
):


temp

=
self
.head


if
temp

is
None
:


return


while
(temp

and
temp.
side by side
):


if
(temp.data !
=
temp.
next
.data):


temp.data, temp.
next
.information

=
temp.
adjacent
.data, temp.information


temp

=
temp.
side by side
.
adjacent


def
push(
self
, new_data):


new_node

=
Node(new_data)


new_node.
next
=
cocky
.head


self
.caput

=
new_node


def
printList(
self
):


temp

=
self
.caput


while
(temp):


print
temp.data,


temp

=
temp.
adjacent

llist

=
LinkedList()

llist.push button(
5
)

llist.push(
iv
)

llist.push(
three
)

llist.button(
2
)

llist.button(
1
)

print
"Linked list earlier calling pairWiseSwap() "

llist.printList()

llist.pairwiseSwap()

print
"\nLinked list subsequently calling pairWiseSwap()"

llist.printList()

C#

using
System;

class
LinkedList {


Node caput;


public
course
Node {


public
int
information;


public
Node next;


public
Node(
int
d)


{


data = d;


next =

zippo
;


}


}


void
pairWiseSwap()


{


Node temp = caput;


while
(temp !=

null
&& temp.next !=

null
) {


int
1000 = temp.data;


temp.data = temp.next.information;


temp.adjacent.data = k;


temp = temp.next.next;


}


}


public
void
push(
int
new_data)


{


Node new_node =

new
Node(new_data);


new_node.next = caput;


head = new_node;


}


void
printList()


{


Node temp = head;


while
(temp !=

null
) {


Console.Write(temp.information +

" "
);


temp = temp.side by side;


}


Console.WriteLine();


}


public
static
void
Main(String[] args)


{


LinkedList llist =

new
LinkedList();


llist.push button(5);


llist.push(iv);


llist.push(three);


llist.push(2);


llist.push(i);


Console.WriteLine(
"Linked List before calling pairWiseSwap() "
);


llist.printList();


llist.pairWiseSwap();


Panel.WriteLine(
"Linked Listing subsequently calling pairWiseSwap() "
);


llist.printList();


}

}

Javascript

<script>

var
caput;


class Node {


constructor(val) {


this
.data = val;


this
.next =

null
;


}


}


part
pairWiseSwap() {

var
temp = head;


while
(temp !=

null
&& temp.next !=

cipher
) {


var
k = temp.information;


temp.data = temp.next.data;


temp.next.data = g;


temp = temp.next.next;


}


}


function
push(new_data) {

var
new_node =

new
Node(new_data);


new_node.next = caput;


head = new_node;


}


role
printList() {

var
temp = head;


while
(temp !=

null
) {


document.write(temp.data +

" "
);


temp = temp.adjacent;


}


document.write(
"<br/>"
);


}


push(5);


push(4);


push button(3);


push(2);


push(ane);


document.write(


"Linked List before calling pairWiseSwap() <br/>"


);


printList();


pairWiseSwap();


document.write(


"Linked List after calling pairWiseSwap()<br/> "


);


printList();

</script>

Output

Linked list before calling pairWiseSwap() i ii 3 4 five  Linked listing later on calling pairWiseSwap() two 1 4 3 v
          

Time complexity:
O(n)

Popular:   Strengths and Weaknesses of Generation Z

METHOD two (Recursive)
If there are ii or more than 2 nodes in Linked Listing then swap the first 2 nodes and recursively call for the rest of the list.

Beneath image is a dry run of the above approach:

Below is the implementation of the above arroyo:

C

void
pairWiseSwap(
struct
node* head)

{


if
(head != Null && head->adjacent != NULL) {


bandy(caput->data, head->side by side->information);


pairWiseSwap(caput->next->next);


}

}

Coffee

static
void
pairWiseSwap(node caput)

{


if
(caput !=

cipher
&& caput.next !=

null
) {


swap(head.data, head.next.data);


pairWiseSwap(caput.next.next);


}

}

Python3

def
pairWiseSwap(head):


if
(head !
=
None
and
caput.
next
!
=
None
):


swap(head.data, head.
next
.data);


pairWiseSwap(caput.
next
.
adjacent
);

C#

static
void
pairWiseSwap(node caput)

{


if
(head !=

null
&& head.next !=

null
) {


bandy(caput.data, head.next.data);


pairWiseSwap(head.next.next);


}

}

Javascript

<script>

function
pairWiseSwap(head)

{


if
(head !=

zippo
&& head.side by side !=

nil
) {


bandy(head.data, head.next.information);


pairWiseSwap(caput.side by side.next);


}

}

</script>

Time complexity:
O(n)

The solution provided in that location swaps data of nodes. If data contains many fields, in that location volition be many swap operations. See
this
for an implementation that changes links rather than swapping data.

Please write comments if y’all find any bug in the higher up code/algorithm, or discover other ways to solve the same problem.

Source: https://www.geeksforgeeks.org/pairwise-swap-elements-of-a-given-linked-list/