3 849
订阅者
无数据24 小时
-297 天
-12730 天
帖子存档
3 849
You are developing a smart device that reads binary data from sensors installed in various locations. Each sensor generates a stream of binary data, and the binary values are stored in a linked list. Your task is to implement a function that calculates the decimal value of the binary data represented by the linked list.
The linked list represents the binary representation of a number, with the most significant bit at the head of the linked list. Your function should traverse the linked list and calculate the decimal value of the binary number.
Note: This is a sample question asked in the Google interview.
Input format :
The input consists of a singly-linked list representing the binary data.
Each node in the linked list contains a value of either 0 or 1.
Output format :
The output is the decimal value calculated from the binary data represented by the linked list.
3 849
// You are using GCC
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int val) : value(val), next(nullptr) {}
};
Node* reverseKNodes(Node* head, int k) {
Node* prev = nullptr;
Node* current = head;
Node* next_node = nullptr;
int count = 0;
while (current != nullptr && count < k) {
next_node = current->next;
current->next = prev;
prev = current;
current = next_node;
count++;
}
if (next_node != nullptr) {
head->next = reverseKNodes(next_node, k);
}
return prev;
}
void printLinkedList(Node* head) {
Node* current = head;
while (current != nullptr) {
std::cout << current->value << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
int N, value, k;
std::cin >> N;
Node* head = nullptr;
Node* current = nullptr;
for (int i = 0; i < N; ++i) {
std::cin >> value;
if (head == nullptr) {
head = new Node(value);
current = head;
} else {
current->next = new Node(value);
current = current->next;
}
}
std::cin >> k;
// Print the original linked list
std::cout << "Original Linked List: ";
printLinkedList(head);
// Reverse every k nodes
head = reverseKNodes(head, k);
// Print the modified linked list
std::cout << "Modified Linked List: ";
printLinkedList(head);
// Clean up memory
while (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
3 849
You are working on a data processing system for a manufacturing company. The company has a production line where items are produced and each item is represented by a node in a linked list. The linked list represents the order in which the items are produced.
Your task is to develop a module that can reverse the order of production for a specific number of items at a time. This will help in optimizing the production process by grouping and processing items in batches.
Note: This is the sample question asked in Paypal recruitment.
Input format :
The first line contains an integer N representing the number of nodes in the linked list.
The second line contains the values of the nodes separated by spaces.
The third line contains an integer representing the value of k.
Output format :
The first line displays the original linked list.
The second line displays the modified linked list after reversing every k node.
3 849
// Make the head of even sublist as the new head of the linked list
head = evenHead;
}
};
// Define the main function
int main() {
// Declare a variable to store the number of elements in the linked list
int n;
// Read the input for n
cin >> n;
// Declare an object of the LinkedList class
LinkedList list;
// Use a loop to insert n elements at the beginning of the linked list
for (int i = 0; i < n; i++) {
// Declare a variable to store the input data for each node
int data;
// Read the input data for each node
cin >> data;
// Insert the node at the beginning of the linked list
list.insertAtBeginning(data);
}
// Rearrange the nodes of the linked list such that even numbers come before odd numbers while maintaining their order within each group
list.rearrangeList();
// Display the contents of the rearranged linked list
list.displayList();
// Return zero to indicate successful termination of the program
return 0;
}
3 849
#include <iostream>
using namespace std;
// Define a class for the node
class Node {
public:
int data; // Data of the node
Node *next; // Pointer to the next node
// Constructor to initialize the node with the given data and next pointer
Node(int data, Node *next = NULL) {
this->data = data;
this->next = next;
}
};
// Define a class for the linked list
class LinkedList {
private:
Node *head; // Pointer to the head of the linked list
public:
// Constructor to initialize the head pointer to NULL
LinkedList() {
head = NULL;
}
// Destructor to delete all the nodes of the linked list
~LinkedList() {
Node *temp = head;
while (temp != NULL) {
Node *del = temp;
temp = temp->next;
delete del;
}
}
// Method to insert a node at the beginning of the linked list
void insertAtBeginning(int data) {
// Create a new node with the given data
Node *newNode = new Node(data);
// Check if the linked list is empty
if (head == NULL) {
// Make the new node as the head of the linked list
head = newNode;
} else {
// Make the new node point to the current head of the linked list
newNode->next = head;
// Make the new node as the new head of the linked list
head = newNode;
}
}
// Method to display the contents of the linked list
void displayList() {
// Check if the linked list is empty
if (head == NULL) {
cout << "Linked List is empty" << endl;
} else {
// Traverse the linked list from head to tail
Node *temp = head;
while (temp != NULL) {
// Print the data of each node followed by a space
cout << temp->data << " ";
// Move to the next node
temp = temp->next;
}
// Print a new line
cout << endl;
}
}
// Method to rearrange the nodes of the linked list such that even numbers come before odd numbers while maintaining their order within each group
void rearrangeList() {
// Check if the linked list is empty or has only one node
if (head == NULL head->next == NULL) {
return; // No need to rearrange
}
// Declare pointers to store the heads and tails of even and odd sublists
Node *evenHead = NULL, *evenTail = NULL, *oddHead = NULL, *oddTail = NULL;
// Traverse the original linked list from head to tail
Node *temp = head;
while (temp != NULL) {
// Check if the current node has an even or odd data value
if (temp->data % 2 == 0) { // Even case
// Check if the even sublist is empty
if (evenHead == NULL) {
// Make the current node as the head and tail of the even sublist
evenHead = evenTail = temp;
} else {
// Append the current node at the end of the even sublist and update its tail pointer
evenTail->next = temp;
evenTail = temp;
}
} else { // Odd case
// Check if the odd sublist is empty
if (oddHead == NULL) {
// Make the current node as the head and tail of the odd sublist
oddHead = oddTail = temp;
} else {
// Append the current node at the end of the odd sublist and update its tail pointer
oddTail->next = temp;
oddTail = temp;
}
}
// Move to the next node in the original linked list
temp = temp->next;
}
// Check if either of the sublists is empty
if (evenHead == NULL oddHead == NULL) {
return; // No need to rearrange further
}
// Concatenate the even and odd sublists and update their pointers accordingly
// Make the tail of even sublist point to the head of odd sublist
evenTail->next = oddHead;
// Make the tail of odd sublist point to NULL
oddTail->next = NULL;
3 849
You are tasked with creating a program that processes a linked list containing integer data and rearranges its nodes. Specifically, your program should separate the even and odd integers, placing the even integers before the odd ones while maintaining their original order within each group.
Note: The new nodes are inserted at the beginning of the linked list.
Example
Input:
linked list = 1 2 3 4
Output:
4 2 3 1
3 849
#include <iostream>
struct Node {
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};
void swapPairs(Node* head) {
Node* current = head;
while (current != nullptr && current->next != nullptr) {
// Swap the data of adjacent nodes
int temp = current->data;
current->data = current->next->data;
current->next->data = temp;
// Move current to the next even node
current = current->next->next;
}
}
void printLinkedList(Node* head) {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
int n;
std::cin >> n;
Node* head = nullptr;
Node* tail = nullptr;
for (int i = 0; i < n; ++i) {
int value;
std::cin >> value;
Node* newNode = new Node(value);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
std::cout << "Linked list before swapping pairwise: ";
printLinkedList(head);
swapPairs(head);
std::cout << "Linked list after swapping pairwise: ";
printLinkedList(head);
// Clean up memory
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
3 849
Imagine you are a teacher preparing seating arrangements for a classroom. You have a list of students' names, represented by a singly linked list. The linked list is arranged in a specific order, but you want to pair up the students in a different way for a group activity.
To achieve this, you need to write a function that swaps elements pairwise in the linked list. Each pair of students will sit together during the activity, fostering collaboration and teamwork. By rearranging the linked list, you can create new pairs of students without changing their individual positions in the list.
For example, if the linked list is 1->2->3->4->5 then the program should change it to 2->1->4->3->5.
Note: This question is asked by Amazon, Microsoft, and Moonfrog Labs.
Input format :
The first line of input consists of the size n.
The second line of input consists of n elements, separated by space.
3 849
#include <iostream>
struct Node {
int data;
Node* next;
Node(int data) : data(data), next(nullptr) {}
};
int calculateDecimalValue(Node* head) {
int decimalValue = 0;
Node* current = head;
while (current != nullptr) {
decimalValue = decimalValue * 2 + current->data;
current = current->next;
}
return decimalValue;
}
void printLinkedList(Node* head) {
Node* current = head;
if (current == nullptr) {
std::cout << "Empty linked list";
return;
}
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
}
int main() {
int n;
std::cin >> n;
Node* head = nullptr;
Node* tail = nullptr;
for (int i = 0; i < n; ++i) {
int value;
std::cin >> value;
Node* newNode = new Node(value);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
std::cout << "Linked List: ";
printLinkedList(head);
std::cout << std::endl;
int decimalValue = calculateDecimalValue(head);
std::cout << "Decimal Value: " << decimalValue << std::endl;
// Clean up memory
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
3 849
You are developing a smart device that reads binary data from sensors installed in various locations. Each sensor generates a stream of binary data, and the binary values are stored in a linked list. Your task is to implement a function that calculates the decimal value of the binary data represented by the linked list.
The linked list represents the binary representation of a number, with the most significant bit at the head of the linked list. Your function should traverse the linked list and calculate the decimal value of the binary number.
Note: This is a sample question asked in the Google interview.
Input format :
The input consists of a singly-linked list representing the binary data.
Each node in the linked list contains a value of either 0 or 1.
3 849
// Make the head of even sublist as the new head of the linked list
head = evenHead;
}
};
// Define the main function
int main() {
// Declare a variable to store the number of elements in the linked list
int n;
// Read the input for n
cin >> n;
// Declare an object of the LinkedList class
LinkedList list;
// Use a loop to insert n elements at the beginning of the linked list
for (int i = 0; i < n; i++) {
// Declare a variable to store the input data for each node
int data;
// Read the input data for each node
cin >> data;
// Insert the node at the beginning of the linked list
list.insertAtBeginning(data);
}
// Rearrange the nodes of the linked list such that even numbers come before odd numbers while maintaining their order within each group
list.rearrangeList();
// Display the contents of the rearranged linked list
list.displayList();
// Return zero to indicate successful termination of the program
return 0;
}
3 849
#include <iostream>
using namespace std;
// Define a class for the node
class Node {
public:
int data; // Data of the node
Node *next; // Pointer to the next node
// Constructor to initialize the node with the given data and next pointer
Node(int data, Node *next = NULL) {
this->data = data;
this->next = next;
}
};
// Define a class for the linked list
class LinkedList {
private:
Node *head; // Pointer to the head of the linked list
public:
// Constructor to initialize the head pointer to NULL
LinkedList() {
head = NULL;
}
// Destructor to delete all the nodes of the linked list
~LinkedList() {
Node *temp = head;
while (temp != NULL) {
Node *del = temp;
temp = temp->next;
delete del;
}
}
// Method to insert a node at the beginning of the linked list
void insertAtBeginning(int data) {
// Create a new node with the given data
Node *newNode = new Node(data);
// Check if the linked list is empty
if (head == NULL) {
// Make the new node as the head of the linked list
head = newNode;
} else {
// Make the new node point to the current head of the linked list
newNode->next = head;
// Make the new node as the new head of the linked list
head = newNode;
}
}
// Method to display the contents of the linked list
void displayList() {
// Check if the linked list is empty
if (head == NULL) {
cout << "Linked List is empty" << endl;
} else {
// Traverse the linked list from head to tail
Node *temp = head;
while (temp != NULL) {
// Print the data of each node followed by a space
cout << temp->data << " ";
// Move to the next node
temp = temp->next;
}
// Print a new line
cout << endl;
}
}
// Method to rearrange the nodes of the linked list such that even numbers come before odd numbers while maintaining their order within each group
void rearrangeList() {
// Check if the linked list is empty or has only one node
if (head == NULL head->next == NULL) {
return; // No need to rearrange
}
// Declare pointers to store the heads and tails of even and odd sublists
Node *evenHead = NULL, *evenTail = NULL, *oddHead = NULL, *oddTail = NULL;
// Traverse the original linked list from head to tail
Node *temp = head;
while (temp != NULL) {
// Check if the current node has an even or odd data value
if (temp->data % 2 == 0) { // Even case
// Check if the even sublist is empty
if (evenHead == NULL) {
// Make the current node as the head and tail of the even sublist
evenHead = evenTail = temp;
} else {
// Append the current node at the end of the even sublist and update its tail pointer
evenTail->next = temp;
evenTail = temp;
}
} else { // Odd case
// Check if the odd sublist is empty
if (oddHead == NULL) {
// Make the current node as the head and tail of the odd sublist
oddHead = oddTail = temp;
} else {
// Append the current node at the end of the odd sublist and update its tail pointer
oddTail->next = temp;
oddTail = temp;
}
}
// Move to the next node in the original linked list
temp = temp->next;
}
// Check if either of the sublists is empty
if (evenHead == NULL oddHead == NULL) {
return; // No need to rearrange further
}
// Concatenate the even and odd sublists and update their pointers accordingly
// Make the tail of even sublist point to the head of odd sublist
evenTail->next = oddHead;
// Make the tail of odd sublist point to NULL
oddTail->next = NULL;
3 849
You are tasked with creating a program that processes a linked list containing integer data and rearranges its nodes. Specifically, your program should separate the even and odd integers, placing the even integers before the odd ones while maintaining their original order within each group.
Note: The new nodes are inserted at the beginning of the linked list.
Example
Input:
linked list = 1 2 3 4
3 849
// You are using GCC
#include <iostream>
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
Node* deleteNodeAtPosition(Node* head, int position) {
if (position <= 0 || !head) {
return head;
}
if (position == 1) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* current = head;
Node* prev = nullptr;
int currentPosition = 1;
while (current && currentPosition < position) {
prev = current;
current = current->next;
currentPosition++;
}
if (current) {
prev->next = current->next;
delete current;
}
return head;
}
void printLinkedList(Node* head) {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
int n;
std::cin >> n;
Node* head = nullptr;
for (int i = 0; i < n; ++i) {
int value;
std::cin >> value;
Node* newNode = new Node(value);
if (!head) {
head = newNode;
} else {
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
}
int positionToDelete;
std::cin >> positionToDelete;
head = deleteNodeAtPosition(head, positionToDelete);
printLinkedList(head);
// Free memory by deleting nodes
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
3 849
You are developing a program for an online shopping cart system. Each node in the singly linked list represents a product added to the cart. The program should allow for deleting a product from the cart at a specific position and displaying the updated cart.
Write a program to implement the online shopping cart system according to the following specifications:
The program should prompt the user to enter the number of products in the cart.
For each product, the program should prompt the user to enter its product code (an integer value).
The program should create a singly linked list with nodes representing the products in the cart, where each node contains the product code.
The program should then prompt the user to enter the position of the product to be deleted from the cart.
The program should delete the node at the specified position from the linked list.
Finally, the program should traverse the updated linked list and print the product codes of the remaining products in the cart.
3 849
// You are using GCC
#include <iostream>
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
Node* insertAtEnd(Node* head, int value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
} else {
Node* current = head;
while (current->next) {
current = current->next;
}
current->next = newNode;
}
return head;
}
Node* deleteCommonNodes(Node* list1, Node* list2) {
Node* current1 = list1;
Node* prev1 = nullptr;
while (current1) {
Node* current2 = list2;
bool found = false;
while (current2) {
if (current1->data == current2->data) {
found = true;
break;
}
current2 = current2->next;
}
if (found) {
if (prev1) {
prev1->next = current1->next;
delete current1;
current1 = prev1->next;
} else {
list1 = current1->next;
delete current1;
current1 = list1;
}
} else {
prev1 = current1;
current1 = current1->next;
}
}
return list1;
}
void printLinkedList(Node* head) {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
int n;
std::cin >> n;
Node* list1 = nullptr;
for (int i = 0; i < n; ++i) {
int value;
std::cin >> value;
list1 = insertAtEnd(list1, value);
}
int m;
std::cin >> m;
Node* list2 = nullptr;
for (int i = 0; i < m; ++i) {
int value;
std::cin >> value;
list2 = insertAtEnd(list2, value);
}
std::cout << "First Linked List before deletion: ";
printLinkedList(list1);
list1 = deleteCommonNodes(list1, list2);
std::cout << "First Linked List after deletion: ";
printLinkedList(list1);
Node* temp = list1;
bool allSame = true;
int prevValue = (temp != nullptr) ? temp->data : 0;
while (temp) {
if (temp->data != prevValue) {
allSame = false;
break;
}
prevValue = temp->data;
temp = temp->next;
}
if (allSame) {
std::cout << "All elements in the first linked list are the same." << std::endl;
}
// Free memory by deleting nodes
while (list1) {
Node* temp = list1;
list1 = list1->next;
delete temp;
}
while (list2) {
Node* temp = list2;
list2 = list2->next;
delete temp;
}
return 0;
}
//awasthi
3 849
Dharun is working on a program to manipulate linked lists. He wants to write a function that takes two linked lists as input, inserts nodes at the end, and deletes all the nodes from the first list that also appear in the second list. Dharun needs your help to implement this function. The function should take two linked lists, list1 and list2, as input, where each list is represented by its head node.
3 849
// You are using GCC
#include <iostream>
// Define the structure for a singly linked list node
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
// Function to create a linked list of student records
Node* createLinkedList(int n) {
if (n <= 0) {
return nullptr;
}
Node* head = nullptr;
Node* tail = nullptr;
for (int i = 0; i < n; ++i) {
int id;
std::cin >> id;
Node* newNode = new Node(id);
if (!head) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// Function to delete the first node from the linked list
Node* deleteFirstNode(Node* head) {
if (!head) {
return nullptr;
}
Node* newHead = head->next;
delete head;
return newHead;
}
// Function to print the linked list
void printLinkedList(Node* head) {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
int main() {
int n;
std::cin >> n;
Node* head = createLinkedList(n);
if (head) {
head = deleteFirstNode(head);
printLinkedList(head);
}
// Free memory by deleting remaining nodes
Node* current = head;
while (current) {
Node* temp = current;
current = current->next;
delete temp;
}
return 0;
}
//awasthi
3 849
You are tasked with developing a program to manage a student records system using a singly linked list. Each node in the linked list represents a student and contains their unique ID. The program should allow for creating a list of student records, deleting the first student's record, and printing the remaining student IDs.
Write a program to implement the student records system according to the following specifications:
The program should prompt the user to enter the number of students n for which records need to be created.
For each student, the program should prompt the user to enter their student ID (an integer value).
The program should create a singly linked list with n nodes, where each node represents a student and contains their student ID.
After creating the linked list, the program should delete the first node from the list.
Finally, the program should traverse the modified linked list and print the student IDs of the remaining students.
3 849
// You are using GCC
#include <iostream>
class Node {
public:
int value;
Node* next;
Node(int val) : value(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void insert(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode;
} else {
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newNode;
}
}
void print() {
Node* current = head;
while (current != nullptr) {
std::cout << current->value << " ";
current = current->next;
}
std::cout << std::endl;
}
void deleteMiddleNode() {
if (head == nullptr head->next == nullptr head->next->next == nullptr) {
return;
}
Node* slow = head;
Node* fast = head;
Node* prev = nullptr;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
prev = slow;
slow = slow->next;
}
prev->next = slow->next;
delete slow;
}
};
int main() {
int numNodes, value;
std::cin >> numNodes;
LinkedList list;
for (int i = 0; i < numNodes; ++i) {
std::cin >> value;
list.insert(value);
}
std::cout << "Original Linked List: ";
list.print();
list.deleteMiddleNode();
std::cout << "Updated Linked List: ";
list.print();
return 0;
}
//awasthi
