#include <bits/stdc++.h> // Common includes
using namespace std; // Standard namespace
// Node Class: Defines structure of a linked list node
int data; // Data storage
Node* next; // Pointer to next node
this->next = NULL; // Initialize next as NULL
// printList: Prints all elements in the list
void printList(Node* head) {
cout << "List is Empty!" << endl;
cout << "Singly List : ";
cout << head->data << " "; // Print data
head = head->next; // Move to next node
// insertAtHead: Adds node to the beginning
void insertAtHead(Node* &head, int data) {
Node *temp = new Node(data); // Create new node
temp->next = head; // New node points to old head
head = temp; // Head updates to new node
// insertAtTail: Adds node to the end
void insertAtTail(Node* &tail, int data) {
Node *temp = new Node(data); // Create new node
tail->next = temp; // Old tail points to new node
tail = temp; // Tail updates to new node
// insertAtMiddle: Inserts node at a specific position
void insertAtMiddle(Node* &head, Node* &tail, int data, int pos) {
if(pos == 1) { // Special case: insert at head
insertAtHead(head, data);
Node *temp = head; // Traverse pointer
while(temp != NULL && pos > 2) { // Find node BEFORE insertion point
if(temp==NULL || pos<=0) { // Invalid position check
cout << "Invalid Position!" << endl;
Node *insertNode = new Node(data); // Create node to insert
insertNode->next = temp->next; // New node points to temp's next
temp->next = insertNode; // Temp points to new node
if(insertNode->next == NULL) { // Update tail if inserted at end
// deletePosition: Deletes node at a specific position
void deletePosition(Node* &head, int pos) {
Node *temp = head; // Pointer for deletion/traversal
if(pos == 1) { // Special case: delete head
delete temp; // Free memory
while(temp!=NULL && pos > 2) { // Find node BEFORE deletion point
if(temp==NULL || temp->next==NULL || pos<=0) { // Invalid position check
cout << "Invalid Position!" << endl;
Node* target = temp->next; // Node to be deleted
temp->next = target->next; // Bypass target node
delete target; // Free memory
// deleteValue: Deletes first occurrence of a specific value
void deleteValue(Node* &head, int val) {
Node* temp = head->next; // Current pointer
Node* prev = head; // Previous pointer
if(head->data == val) { // Special case: delete head by value
delete prev; // Free old head
// Traverse to find the value
val = INT_MIN; // Sentinel to mark value found
prev = temp; // Move prev
temp = temp->next; // Move temp
if(prev->next==NULL || val!=INT_MIN) { // Value not found
cout << "Value Not Found!" << endl;
prev->next = temp->next; // Bypass target node
delete temp; // Free memory
// reverseInGroup: Reverses the list in groups of 'k' nodes (Recursive)
Node* reverseInGroup(Node* head, int k) {
if(head == NULL) { // Base case: empty list
// STEP 1: Reverse K Nodes (Iterative within current group)
Node* next = NULL; // To store next node after current
Node* curr = head; // Current node
Node* prev = NULL; // Previous node (for reversal)
int count = 0; // Counter for current group
while(curr!=NULL && count<k) {
next = curr->next; // Save next
curr->next = prev; // Reverse current node's pointer
prev = curr; // Move prev forward
curr = next; // Move curr forward
count++; // Increment count
// STEP 2: Recursive call for remaining list
// Original head (now tail of reversed group) points to reversed next group
head->next = reverseInGroup(next, k);
// STEP 3: Return new head of this reversed group
// main: Program entry point, demonstrates operations
Node *head = new Node(1); // Create initial node
Node *tail = head; // Tail points to head initially
// Build list: 1 -> 2 -> 4 -> 8 -> 16 -> 32
for(int i=1; i<=5; i++) {
insertAtTail(tail, pow(2,i));
printList(head); // Print original list
// Reverse in groups of 2
head = reverseInGroup(head, 2);
printList(head); // Print list after reversal
return 0; // Indicate success