একটি সিংলি লিঙ্কড লিস্ট দেওয়া থাকলে, তার প্রথম নোড (হেড) ডিলিট করতে হবে।
ইনপুট ১
4 → 3 → 2 → 1
আউটপুট
3 → 2 → 1 →
ইনপুট ২
1 → 2 → 3 →
আউটপুট
2 → 3 →
সমাধানের পদ্ধতি
deleteAtFirst(Node* &head)ফাংশন হেডের রেফারেন্স নেবে।- যদি হেড NULL হয়, রিটার্ন করুন।
temp = head; head = head->next; delete temp;করুন।- লিস্ট প্রিন্ট করুন।
উদাহরণ
#include <iostream>
using namespace std;
class Node {
public:
int data; Node* next;
Node(int d) { data = d; next = NULL; }
};
void insertAtFirst(Node* &head, int data) {
Node* n = new Node(data);
n->next = head; head = n;
}
void print(Node* head) {
while (head != NULL) {
cout << head->data << "→";
head = head->next;
}
cout << endl;
}
void deleteAtFirst(Node* &head) {
if (head == NULL) return;
Node* temp = head;
head = head->next;
delete temp;
}
int main() {
Node* head = NULL;
insertAtFirst(head, 1); insertAtFirst(head, 2);
insertAtFirst(head, 3); insertAtFirst(head, 4);
deleteAtFirst(head);
print(head);
return 0;
}
আউটপুট
3 → 2 → 1 →