Given a linked list containing N nodes of binary numbers, the task is to check whether it is possible to rearrange the linked list in such a way that value of XOR between element at ith Node and N+1−ith node is the same for all 1 ≤ i ≤ N. Print Yes, if the Linked List can be rearranged, or else print No.
Examples:
Input: LL = 0 -> 0 -> 1
Output: Yes
Explanation : 001 can be rearranged to form 010 which means 0 ⊕ 0 = 0 and 1 ⊕ 1 =0 are same . So output is 1.
Input : 0001 (0->0->0->1)
Output : No
Explanation : 0001 can not be rearranged . So output is 0.
Approach: The idea is based on the linked list traversal, based on following observations:
Case 1: If size of linked linked list is odd, then it is always possible. So directly return Yes.
For example:
- 00001 can be rearranged as 00100
- 00011 can be rearranged as 10001
- 00111 can be rearranged as 10101
- 01111 can be rearranged as 11011
Case 2: If size of linked linked list is even, Count the number of 1 present in linked list. If number of 1s present in linked list is equal to the half the size of linked list, then return Yes, because rearrangement will be always possible.
For example:
- 0011
number of 1’s = half of size of linked list
so it can be rearranged as 1001
- 001101
number of 1’s = half of size of linked list
so it can be rearranged as 010101
Case 3: if number of 1’s present in our linked list is even, then also it is always possible. So directly return Yes.
For example:
- 000011 can be rearranged as 100001 or 001100
- 011 can be rearranged as 101
Case 4: if above all conditions are false, then return No.
For example:
- 011111 cannot be rearranged
- 000001 cannot be rearranged
Follow the below steps to implement the above approach:
- Calculate the size of linked list.
- If size is odd then return 1 because it is always possible to rearrange linked list as discuss above.
- If size is even, count the number of node consist value 1.
- If count of number of node consist value 1 is half of the size of linked list then return 1.
- If size is even and count of number of node consist value 1 is even then return 1.
- If all the above condition is fail to execute then return 0.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return ;
}
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
return ;
}
int CountSize(Node* head)
{
Node* temp = head;
int count = 0;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
bool isPossible(Node* head)
{
int n = CountSize(head);
if (n % 2 != 0) {
return 1;
}
else {
int o = 0;
Node* temp = head;
while (temp != NULL) {
if (temp->data == 1) {
o++;
}
temp = temp->next;
}
if (o == (n / 2)) {
return 1;
}
else if (o % 2 == 0) {
return 1;
}
else {
return 0;
}
}
}
int main()
{
Node* head = NULL;
append(&head, 0);
append(&head, 0);
append(&head, 1);
append(&head, 1);
cout << (isPossible(head) == 1
? "Yes"
: "No" )
<< endl;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
static int CountSize(Node head) {
Node temp = head;
int count = 0 ;
while (temp != null ) {
count++;
temp = temp.next;
}
return count;
}
static int isPossible(Node head) {
int n = CountSize(head);
if (n % 2 != 0 ) {
return 1 ;
}
else {
int o = 0 ;
Node temp = head;
while (temp != null ) {
if (temp.data == 1 ) {
o++;
}
temp = temp.next;
}
if (o == Math.floor(n / 2 )) {
return 1 ;
}
else if (o % 2 == 0 ) {
return 1 ;
}
else {
return 0 ;
}
}
}
public static void main(String[] args)
{
Node head = new Node( 1 );
head.next = new Node( 1 );
head.next.next = new Node( 0 );
head.next.next.next = new Node( 0 );
System.out.println((isPossible(head) == 1
? "Yes"
: "No" ));
}
}
|
Python3
class Node:
def __init__( self ,d):
self .data = d
self . next = None
def CountSize(head):
temp = head
count = 0
while (temp ! = None ):
count + = 1
temp = temp. next
return count
def isPossible(head):
n = CountSize(head)
if (n % 2 ! = 0 ):
return 1
else :
o = 0
temp = head
while (temp ! = None ):
if (temp.data = = 1 ):
o + = 1
temp = temp. next
if (o = = (n / / 2 )):
return 1
elif (o % 2 = = 0 ):
return 1
else :
return 0
head = Node( 1 )
head. next = Node( 1 )
head. next . next = Node( 0 )
head. next . next . next = Node( 0 )
print ( "Yes" if (isPossible(head) = = 1 ) else "No" )
|
C#
using System;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public class LinkedList{
public Node head;
public int CountSize()
{
Node temp = head;
int count = 0;
while (temp != null ) {
count++;
temp = temp.next;
}
return count;
}
public Node append(Node node, int val)
{
Node new_node = new Node(val);
node.next = new_node;
return new_node;
}
}
public class GFG
{
public static bool isPossible(LinkedList LL)
{
int n = LL.CountSize();
if (n % 2 != 0) {
return true ;
}
else
{
int o = 0;
Node temp = LL.head;
while (temp != null ) {
if (temp.data == 1) {
o++;
}
temp = temp.next;
}
if (o == (n / 2)) {
return true ;
}
else if (o % 2 == 0) {
return true ;
}
else {
return false ;
}
}
}
public static void Main()
{
LinkedList llist = new LinkedList();
llist.head = new Node(0);
Node cur = llist.head;
cur = llist.append(cur, 0);
cur = llist.append(cur, 1);
cur = llist.append(cur, 1);
if (isPossible(llist))
{
Console.Write( "Yes\n" );
}
else
{
Console.Write( "No\n" );
}
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
};
function CountSize(head) {
let temp = head;
let count = 0;
while (temp != null ) {
count++;
temp = temp.next;
}
return count;
}
function isPossible(head) {
let n = CountSize(head);
if (n % 2 != 0) {
return 1;
}
else {
let o = 0;
let temp = head;
while (temp != null ) {
if (temp.data == 1) {
o++;
}
temp = temp.next;
}
if (o == Math.floor(n / 2)) {
return 1;
}
else if (o % 2 == 0) {
return 1;
}
else {
return 0;
}
}
}
let head = new Node(1);
head.next = new Node(1);
head.next.next = new Node(0)
head.next.next.next = new Node(0)
document.write((isPossible(head) == 1
? "Yes"
: "No" )
+ '<br>' )
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!