struct Node {
int data;
Node* next;
};
void deleteAtBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
deleteAtBeginning(head);
printList(head);
return 0;
}Initially, the list is 1 -> 2 -> 3 -> null.
Deleting the first node removes the node with data 1.
The head now points to the node with data 2, so the list becomes 2 -> 3 -> null.
struct Node {
int data;
Node* next;
Node* prev;
};
void deleteAtBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
if (head != nullptr) head->prev = nullptr;
delete temp;
}
void printList(Node* head) {
while (head != nullptr) {
std::cout << head->data << " <-> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* third = new Node{3, nullptr, nullptr};
Node* second = new Node{2, third, nullptr};
third->prev = second;
Node* head = new Node{1, second, nullptr};
second->prev = head;
deleteAtBeginning(head);
printList(head);
return 0;
}The original list is 1 <-> 2 <-> 3 <-> null.
Deleting the first node removes the node with data 1.
The head now points to the node with data 2, and its prev pointer is set to nullptr.
The list becomes 2 <-> 3 <-> null.
struct Node {
int data;
Node* next;
};
void deleteAtBeginning(Node* head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
}
int main() {
Node* head = new Node{1, new Node{2, nullptr}};
deleteAtBeginning(head);
std::cout << head->data << std::endl;
return 0;
}The function receives head by value, so the original head pointer in main is not updated.
After deleting the first node, main still uses the old head pointer which points to deleted memory.
This causes a runtime error when accessing head->data.
struct Node {
int data;
Node* next;
};
void deleteAtBeginning(Node*& head) {
if (head == nullptr) return;
if (head->next == head) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
Node* tail = head;
while (tail->next != head) {
tail = tail->next;
}
head = head->next;
tail->next = head;
delete temp;
}
void printList(Node* head) {
if (head == nullptr) {
std::cout << "null" << std::endl;
return;
}
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "(back to head)" << std::endl;
}
int main() {
Node* third = new Node{3, nullptr};
Node* second = new Node{2, third};
Node* head = new Node{1, second};
third->next = head;
deleteAtBeginning(head);
printList(head);
return 0;
}The circular list is 1 -> 2 -> 3 -> back to 1.
Deleting the first node removes 1.
The tail node (3) now points to the new head (2).
The list becomes 2 -> 3 -> back to 2.
Deleting the first node removes exactly one node from the list.
Therefore, the length decreases by one, from N to N - 1.
