Exchange First and Last Nodes in Circular Linked List: Filter - None Edit Play - Arrow Brightness - 4
Exchange First and Last Nodes in Circular Linked List: Filter - None Edit Play - Arrow Brightness - 4
Exchange First and Last Nodes in Circular Linked List: Filter - None Edit Play - Arrow Brightness - 4
Given a Circular linked list exchange the first and the last node. The task should be
done with only one extra node, you can not declare more than one extra node and
also you are not allowed to declare any other temporary variable.
Note: Extra node means need of a node to traverse a list.
Examples:
Input : 5 4 3 2 1
Output : 1 4 3 2 5
Input : 6 1 2 3 4 5 6 7 8 9
Output : 9 1 2 3 4 5 6 7 8 6
We first find pointer to previous of last node. Let this node be p. Now we change next
links so that the last and first nodes are swapped.
C++
Java
C#
filter_none
edit
play_arrow
brightness_4
// CPP program to exchange first and
// last node in circular linked list
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
return head;
}
return head;
}
} while(p != head);
}
return head;
}
// Driven Program
int main()
{
int i;
struct Node *head = NULL;
head = addToEmpty(head, 6);
return 0;
}
Output:
List Before: 6 1 2 3 4 5
List After: 5 1 2 3 4 6
Split a Circular Linked List into two halves
if(head == NULL)
return;
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of a Circular
linked list */
void push(struct Node **head_ref, int data)
{
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
*head_ref = ptr1;
}
getchar();
return 0;
}
Output:
Original Circular Linked List
11 2 56 12
First Circular Linked List
11 2
Second Circular Linked List
56 12
Time Complexity: O(n)
Algorithm:
Allocate memory for the newly inserted node and put data in the newly allocated
node. Let the pointer to the new node be new_node. After memory allocation,
following are the three cases that need to be handled.
1) Linked List is empty:
a) since new_node is the only node in CLL, make a self loop.
new_node->next = new_node;
b) change the head pointer to point to new node.
*head_ref = new_node;
2) New node is to be inserted just before the head node:
(a) Find out the last node using a loop.
while(current->next != *head_ref)
current = current->next;
(b) Change the next of last node.
current->next = new_node;
(c) Change next of new node to point to head.
new_node->next = *head_ref;
(d) change the head pointer to point to new node.
*head_ref = new_node;
3) New node is to be inserted somewhere after the head:
(a) Locate the node after which new node is to be inserted.
while ( current->next!= *head_ref &&
current->next->data data)
{ current = current->next; }
(b) Make next of new_node as next of the located pointer
new_node->next = current->next;
(c) Change the next of the located pointer
current->next = new_node;
new_node->next = current->next;
current->next = new_node;
}
}
if(start != NULL)
{
temp = start;
do {
cout<<temp->data<<" ";
temp = temp->next;
} while(temp != start);
}
}
/* Driver code */
int main()
{
int arr[] = {12, 56, 2, 11, 1, 90};
int list_size, i;
printList(start);
return 0;
}
Output:
1 2 11 12 56 90
Time Complexity: O(n) where n is the number of nodes in the given linked list.
return 0;
}
Output:
Original Doubly linked list:
2 5 7 12 10 6 4 1
Doubly linked list after sorting:
1 2 4 5 6 7 10 12
Time Complexity: O(n)
C++
Java
Python3
C#
filter_none
edit
play_arrow
brightness_5
// C++ Program to convert a Binary Tree
#include<iostream>
int data;
};
// of leftList.
if (leftList == NULL)
return rightList;
if (rightList == NULL)
return leftList;
leftLast->right = rightList;
rightList->left = leftLast;
leftList->left = rightLast;
rightLast->right = leftList;
return leftList;
if (root == NULL)
return NULL;
// right List)
do
itr = itr->right;
} while (head!=itr);
temp->data = data;
return temp;
int main()
root->left = newNode(12);
root->right = newNode(15);
root->left->left = newNode(25);
root->left->right = newNode(30);
root->right->left = newNode(36);
displayCList(head);
return 0;
}
Output:
Circular Linked List is :
25 12 30 10 36 15
Input : n = 5
m = 3
Output : 4
We have discussed different solutions of this problem (here and here). In this post a
simple circular linked list based solution is discussed.
1) Create a circular linked list of size n.
2) Traverse through linked list and one by one delete every m-th node until there is one node
left.
3) Return value of the only left node.
// CPP program to find last man standing
#include<bits/stdc++.h>
using namespace std;