Linked List Insert

Linked List Insert

Medium C Structures 30 views
Explanation Complexity

Problem Statement

Create Node structure (data, next pointer). Create Functions: insertAtBeginning(), insertAtEnd(), insertAtPosition(), display(). Proper pointer manipulation.

Input Format

Operations on a singly linked list:

• insert at beginning

• insert at end

• insert at given position

Output Format

Linked list elements printed in order.

Example

Operations:

• insertAtBeginning(10)

• insertAtEnd(30)

• insertAtPosition(20, 2)
10 20 30

Constraints

• Linked list is singly linked

• Position starts from 1

• Position must be valid

Concept Explanation

Each node has data and a pointer to the next node.
Insertion is done by changing pointers carefully.

Step-by-Step Explanation

Node Structure

1.Create Node with:

• data

• next pointer

insertAtBeginning(value)

1.Create a new node.

2.Set new node’s data = value.

3.Set new node’s next to current head.

4.Update head to new node.

insertAtEnd(value)

1.Create a new node.

2.Set new node’s data = value and next = NULL.

3.If list is empty:

• Make new node the head.

4.Else:

• Traverse to the last node.

• Set last node’s next to new node.

insertAtPosition(value, position)

1.Create a new node.

2.If position is 1:

• Call insertAtBeginning.

3.Else:

• Traverse to node at (position − 1).

4.Set new node’s next to current node’s next.

5.Set current node’s next to new node.

display()

1.Set a pointer temp to head.

2.While temp is not NULL:

• Print temp->data.

• Move temp = temp->next.

Concept Explanation

Each node has data and a pointer to the next node.
Insertion is done by changing pointers carefully.

Step-by-Step Explanation

Node Structure

1.Create Node with:

• data

• next pointer

insertAtBeginning(value)

1.Create a new node.

2.Set new node’s data = value.

3.Set new node’s next to current head.

4.Update head to new node.

insertAtEnd(value)

1.Create a new node.

2.Set new node’s data = value and next = NULL.

3.If list is empty:

• Make new node the head.

4.Else:

• Traverse to the last node.

• Set last node’s next to new node.

insertAtPosition(value, position)

1.Create a new node.

2.If position is 1:

• Call insertAtBeginning.

3.Else:

• Traverse to node at (position − 1).

4.Set new node’s next to current node’s next.

5.Set current node’s next to new node.

display()

1.Set a pointer temp to head.

2.While temp is not NULL:

• Print temp->data.

• Move temp = temp->next.

Input / Output Format

Input Format
Operations on a singly linked list:

• insert at beginning

• insert at end

• insert at given position
Output Format
Linked list elements printed in order.
Constraints
• Linked list is singly linked

• Position starts from 1

• Position must be valid

Examples

Input:
Operations: • insertAtBeginning(10) • insertAtEnd(30) • insertAtPosition(20, 2)
Output:
10 20 30

Example Solution (Public)

C
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
    int data;
    struct Node *next;
};

// Insert at beginning
void insertAtBeginning(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = *head;
    *head = newNode;
}

// Insert at end
void insertAtEnd(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = *head;

    newNode->data = value;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}

// Insert at specific position (1-based index)
void insertAtPosition(struct Node **head, int value, int position) {
    int i;
    struct Node *temp = *head;

    if (position == 1) {
        insertAtBeginning(head, value);
        return;
    }

    for (i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Invalid position!n");
        return;
    }

    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = temp->next;
    temp->next = newNode;
}

// Display list
void display(struct Node *head) {
    if (head == NULL) {
        printf("List is empty.n");
        return;
    }

    printf("Linked List: ");
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULLn");
}

// Main function
int main() {
    struct Node *head = NULL;

    insertAtBeginning(&head, 10);
    insertAtEnd(&head, 20);
    insertAtEnd(&head, 30);
    insertAtPosition(&head, 25, 3);

    display(head);

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
    int data;
    struct Node *next;
};

// Insert at beginning
void insertAtBeginning(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = *head;
    *head = newNode;
}

// Insert at end
void insertAtEnd(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *temp = *head;

    newNode->data = value;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}

// Insert at specific position (1-based index)
void insertAtPosition(struct Node **head, int value, int position) {
    int i;
    struct Node *temp = *head;

    if (position == 1) {
        insertAtBeginning(head, value);
        return;
    }

    for (i = 1; i < position - 1 && temp != NULL; i++) {
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Invalid position!n");
        return;
    }

    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = temp->next;
    temp->next = newNode;
}

// Display list
void display(struct Node *head) {
    if (head == NULL) {
        printf("List is empty.n");
        return;
    }

    printf("Linked List: ");
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULLn");
}

// Main function
int main() {
    struct Node *head = NULL;

    insertAtBeginning(&head, 10);
    insertAtEnd(&head, 20);
    insertAtEnd(&head, 30);
    insertAtPosition(&head, 25, 3);

    display(head);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.