Complete the code to create a linear linked list node pointing to null.
struct Node {
int data;
Node* next = [1];
};In modern C++, nullptr is used to indicate the end of a linear linked list.
Complete the code to make the last node point back to the head, forming a circular linked list.
void makeCircular(Node* last, Node* head) {
last->next = [1];
}In a circular linked list, the last node points back to the head node.
Fix the error in the loop condition to traverse a circular linked list correctly.
void traverseCircular(Node* head) {
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != [1]);
std::cout << "null";
}In circular linked list traversal, stop when you reach the head again.
Fill both blanks to create a linear linked list and check if it is circular.
bool isCircular(Node* head) {
if (head == [1]) return false;
Node* temp = head->next;
while (temp != [2] && temp != nullptr) {
temp = temp->next;
}
return temp == head;
}Check if head is null first, then traverse until you find head again or reach null.
Fill all three blanks to insert a new node at the end of a circular linked list.
void insertEnd(Node*& head, int value) {
Node* newNode = new Node{value, [1];
if (head == [2]) {
head = newNode;
newNode->next = newNode;
} else {
Node* temp = head;
while (temp->next != [3]) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}New node initially points to nullptr. If list empty (head is nullptr), set head to new node and point new node to itself. Otherwise, find last node (where next points to head) and insert new node there.
