swap two nodes in linked list

Solutions on MaxInterview for swap two nodes in linked list by the best coders in the world

showing results for - "swap two nodes in linked list"
Martina
09 May 2019
1//not changing the data inside
2//Try on your own first
3#include<iostream>
4using namespace std;
5struct node
6{
7  int data;
8  node* next;
9};
10node * head=NULL;
11void insertion(int key)
12{
13  node *temp=new node;
14  temp->data=key;
15  temp->next=NULL;
16  if(head==NULL)
17  {
18    head=temp;
19  }
20  else
21  {
22    node* ptr=head;
23    while(ptr->next!=NULL)
24    {
25      ptr=ptr->next;
26    }
27    ptr->next=temp;
28  }
29}
30void swapnode(int x,int y)
31{
32  if(x==y)
33  {
34    return;
35  }
36  node *x_prev=NULL;
37  node* x_curr=head;
38  node* y_prev=NULL;
39  node* y_curr=head;
40  while(x_curr!=NULL&&x_curr->data!=x)
41  {
42    x_prev=x_curr;
43    x_curr=x_curr->next;
44  }
45  while(y_curr!=NULL&&y_curr->data!=y)
46  {
47    y_prev=y_curr;
48    y_curr=y_curr->next;
49  }
50  if(x_curr==NULL||y_curr==NULL)
51  {
52    return;
53  }
54  if(x_prev!=NULL)
55  {
56    x_prev->next=y_curr;
57  }
58  else
59  {
60    head=y_curr;
61  }
62  if(y_prev!=NULL)
63  {
64    y_prev->next=x_curr;
65  }
66  else
67  {
68    head=x_curr;
69  }
70  node *temp=y_curr->next;
71  y_curr->next=x_curr->next;
72  x_curr->next=temp;
73}
74void print()
75{
76  node* temp=head;
77  while(temp!=NULL)
78  {
79    cout<<temp->data<<" ";
80    temp=temp->next;
81  }
82}
83int main()
84{
85  int n;
86  cin>>n;
87  int value;
88  for(int i=0;i<n;i++)
89  {
90    cin>>value;
91    insertion(value);
92  }
93  int p,q;
94  cin>>p>>q;
95  swapnode(p,q);
96  print();
97  return 0;
98}
99
queries leading to this page
swap alternate nodes in a linked listswap k nodes from end in linked listswap 2 nodes in linked list pythonswap alternate nodes in a singly linked listcan we swap the position of nodes in linked listswap every 2 items in linked listswap nodes in pairs linkedlistswapping nodes in a doubly linked listswap two nodes in linked list javasort linked list by swapping nodesswapping two nodes in linked listhow to swap two nodes in a linkes list c 2b 2bswap nodes in a linked list without swapping dataswap the pair i linked listswapping node values in linked listswap two nodes in a linked listhow to swap two nodes in a linked list c 2b 2bswap nodes of a linked list gfg practiceswap node in linked list javaswap nodes linked listswap nodes linked lisswapnodes 28 29 in doubly linked list without swapping dataswap elements in linked listswap nodes in pairs linked listswap k nodes in linked listswap alternate nodes linked listswapping nodes in a linked listhow to swap two nodes in link listsort a linked list by swaping nodesswap all in linked listswapping two nodes in a singly linked list cswap linked list in pairshow to swap nodes in a singly linked listpairwise swap of elements in linked listhow to swap two nodes in a singly linked listhow to swap two nodes in a linked listlinked list swappingswapping between 2 nodes in doubly linkedlistswapping linked list nodes in pythonswapping value in linked list c 2b 2bswap nodes in a linked list without swapping data in cswap nodes in a linked listswap k nodes in lined listswapping between 2 nodes in linkedlistlinked list swap nodesswap 2 nodes in linked listswap node in linked listswap nodes in linked list gfg practicehow to swap two nodes in linked listswap a linked list with data1721 swapping nodes in a linked listswap nodes in linked listswap nodes in a linked lsitreverse a linked list by swapping nodesswap two nodes of linked listswap every two nodes in a linked listswap pairwise nodes in linked listhow to swap two nodes in linked list in cswap adjacent nodes in linked listswap two adjacent nodes in linked listswap linked listlinked list swap adjacent nodesswapping nodes at kth position linkedlistpairwise swap in linked listswap elements in doubly linked listswap two nodes of doubly linked list in javaswap two linked list nodesswapping position in linked listswap in linked listswap nodes of a linked listswap two nodes in linked listswap linked list nodesswap two nodes in single linkedlistswap two nodes in linked list leetcodeswap two adjacent nodes singly linked listswap kth nodes from start and end linked listswapping two nodes in a singly linked list javapair swap linked listhow to swap nodes in a doubly linked listswap linked list nodes in pairsswap two nodes in linked list