将单链表转换为异或链表
先决条件:
- XOR 链表——内存高效的双向链表 |设置 1
- XOR 链表——内存高效的双向链表 | 2套
XOR 链表是一种内存高效的双向链表,其中每个节点的下一个指针存储前一个和下一个节点地址的 XOR。
给定一个单向链表,任务是将给定的单向链表转换为 XOR 链表。
方法:由于XOR链接列表中的每个下一个指针店prev的XOR和下一个节点的地址。所以这个想法是遍历给定的单向链表并跟踪指针中的前一个节点,比如prev 。
现在,在遍历列表时,将每个节点的下一个指针更改为:
current -> next = XOR(prev, current->next)
打印异或链表:
在打印异或链表时,我们每次都必须找到下一个节点的确切地址。正如我们在上面看到的,每个节点的下一个指针存储上一个和下一个节点地址的异或值。因此,可以通过在异或链表中找到当前节点的prev 和next 指针的异或来获得下一个节点的地址。
因此,要打印 XOR 链表,通过维护一个存储前一个节点地址的 prev 指针遍历它并找到下一个节点,计算 prev 与当前节点的 next 的 XOR。
下面是上述方法的实现:
CPP
// C++ program to Convert a Singly Linked
// List to XOR Linked List
#include
using namespace std;
// Linked List node
struct Node {
int data;
struct Node* next;
};
// Utiltity function to create new node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
// Print singly linked list before conversion
void print(Node* head)
{
while (head) {
// print current node
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
// Function to find XORed value of
// the node addresses
Node* XOR(Node* a, Node* b)
{
return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
// Function to convert singly linked
// list to XOR linked list
void convert(Node* head)
{
Node* curr = head;
Node* prev = NULL;
Node* next = curr->next;
while (curr) {
// store curr->next in next
next = curr->next;
// cahnge curr->next to XOR of prev and next
curr->next = XOR(prev, next);
// prev wil change to curr for next iteration
prev = curr;
// curr is now pointing to next for next iteration
curr = next;
}
}
// Function to print XORed liked list
void printXOR(Node* head)
{
Node* curr = head;
Node* prev = NULL;
while (curr) {
// print current node
cout << curr->data << " ";
Node* temp = curr;
/* compute curr as prev^curr->next as
it is previously set as prev^curr->next so
this time curr would be prev^prev^curr->next
which is curr->next */
curr = XOR(prev, curr->next);
prev = temp;
}
cout << endl;
}
// Driver Code
int main()
{
// Create following singly linked list
// 1->2->3->4
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
cout << "Before Conversion : " << endl;
print(head);
convert(head);
cout << "After Conversion : " << endl;
printXOR(head);
return 0;
}
Java
// Java program to Convert a Singly Linked
// List to XOR Linked List
import java.io.*;
// Linked List node
class Node
{
int data;
Node next;
// Utiltity function to create new node
Node(int item)
{
data = item;
next = null;
}
}
class GFG
{
public static Node root;
// Print singly linked list before conversion
static void print(Node head)
{
while (head != null)
{
// print current node
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
// Function to find XORed value of
// the node addresses
static Node XOR(Node a, Node b)
{
return b;
}
// Function to convert singly linked
// list to XOR linked list
static void convert(Node head)
{
Node curr = head;
Node prev = null;
Node next = curr.next;
while(curr != null)
{
// store curr->next in next
next = curr.next;
// cahnge curr->next to XOR of prev and next
curr.next = XOR(prev, next);
// prev wil change to curr for next iteration
prev = curr;
// curr is now pointing to next for next iteration
curr = next;
}
}
// Function to print XORed liked list
static void printXOR(Node head)
{
Node curr = head;
Node prev = null;
while(curr != null)
{
// print current node
System.out.print(curr.data + " ");
Node temp = curr;
/* compute curr as prev^curr->next as
it is previously set as prev^curr->next so
this time curr would be prev^prev^curr->next
which is curr->next */
curr = XOR(prev, curr.next);
prev = temp;
}
System.out.println();
}
// Driver Code
public static void main (String[] args)
{
// Create following singly linked list
// 1->2->3->4
GFG tree = new GFG();
tree.root = new Node(1);
tree.root.next = new Node(2);
tree.root.next.next = new Node(3);
tree.root.next.next.next = new Node(4);
System.out.println("Before Conversion : ");
print(root);
convert(root);
System.out.println("After Conversion : ");
printXOR(root);
}
}
// This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to Convert a Singly Linked
# List to XOR Linked List
# Linked List node
class Node:
def __init__(self,d):
self.data = d
self.next = None
# Print singly linked list before conversion
def printt(head):
while (head):
# print current node
print(head.data, end=" ")
head = head.next
print()
# Function to find XORed value of
# the node addresses
def XOR(a, b):
return b
# Function to convert singly linked
# list to XOR linked list
def convert(head):
curr = head
prev = None
next = curr.next
while (curr):
# store curr.next in next
next = curr.next
# cahnge curr.next to XOR of prev and next
curr.next = XOR(prev, next)
# prev wil change to curr for next iteration
prev = curr
# curr is now pointing to next for next iteration
curr = next
# Function to print XORed liked list
def printXOR(head):
curr = head
prev = None
while (curr):
# print current node
print(curr.data, end=" ")
temp = curr
# /* compute curr as prev^curr.next as
# it is previously set as prev^curr.next so
# this time curr would be prev^prev^curr.next
# which is curr.next */
curr = XOR(prev, curr.next)
prev = temp
print()
# Driver Code
if __name__ == '__main__':
# Create following singly linked list
# 1.2.3.4
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
print("Before Conversion : ")
printt(head)
convert(head)
print("After Conversion : ")
printXOR(head)
# This code is contributed by mohitkumar29
C#
using System;
class Node
{
public int data;
public Node next;
// Utiltity function to create new node
public Node(int item)
{
data = item;
next = null;
}
}
public class GFG
{
static Node root;
// Print singly linked list before conversion
static void print(Node head)
{
while (head != null)
{
// print current node
Console.Write(head.data + " ");
head = head.next;
}
Console.WriteLine();
}
// Function to find XORed value of
// the node addresses
static Node XOR(Node a, Node b)
{
return b;
}
// Function to convert singly linked
// list to XOR linked list
static void convert(Node head)
{
Node curr = head;
Node prev = null;
Node next = curr.next;
while(curr != null)
{
// store curr->next in next
next = curr.next;
// cahnge curr->next to XOR of prev and next
curr.next = XOR(prev, next);
// prev wil change to curr for next iteration
prev = curr;
// curr is now pointing to next for next iteration
curr = next;
}
}
// Function to print XORed liked list
static void printXOR(Node head)
{
Node curr = head;
Node prev = null;
while(curr != null)
{
// print current node
Console.Write(curr.data + " ");
Node temp = curr;
/* compute curr as prev^curr->next as
it is previously set as prev^curr->next so
this time curr would be prev^prev^curr->next
which is curr->next */
curr = XOR(prev, curr.next);
prev = temp;
}
Console.WriteLine();
}
// Driver Code
static public void Main ()
{
// Create following singly linked list
// 1->2->3->4
GFG.root = new Node(1);
GFG.root.next = new Node(2);
GFG.root.next.next = new Node(3);
GFG.root.next.next.next = new Node(4);
Console.WriteLine("Before Conversion : ");
print(root);
convert(root);
Console.WriteLine("After Conversion : ");
printXOR(root);
}
}
// This code is contributed by rag2127
Javascript
输出:
Before Conversion :
1 2 3 4
After Conversion :
1 2 3 4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。