Skip to content

Singly Linked List

Singly Linked List

It is the simplest type of linked list in which every node contains some data and a pointer to the next node of the same data type.

loading...


Implementation

See the code
#include <bits/stdc++.h> 
using namespace std; 

// Structure of Node
class Node{ 
  public: 
    int data;
    Node* next;

    Node(int data){
        this ->data = data;
        this ->next = NULL;
    }
}; 

Node* constructLL(vector<int>& arr) {

    if(arr.size() == 0){
        return NULL;
    }

    Node *Head = new Node(arr[0]);
    Node *temp = Head;

    for(int i = 1 ; i < arr.size() ; i++){
        Node* new_node = new Node(arr[i]);
        temp ->next = new_node;
        temp = temp ->next;
    }

    return Head;
}

void printList(Node* head){ 

    Node* temp = head;
    while (temp != NULL) { 

        cout << temp->data << " "; 
        temp = temp->next;
    } 
}

int main(){ 

    vector<int> arr = {1,2,3,4,5};

    Node *head = constructLL(arr);
    printList(head);
    cout<<endl;

    return 0; 
}

Questions

💡 First try with yourself, if you are unable to solve the question then see the solution.

Linked List Insertion
class Solution{
public:
    //Function to insert a node at the beginning of the linked list.
    Node *insertAtBegining(Node *head, int x) {

        Node* new_node = new Node(x);
        new_node ->next = head;
        return new_node;
    }


    //Function to insert a node at the end of the linked list.
    Node *insertAtEnd(Node *head, int x)  {

        Node* new_node = new Node(x);
        if(head == NULL){
            return new_node;
        }
        Node* temp = head;
        Node* last = NULL;
        while(temp != NULL){
            last = temp;
            temp = temp ->next;
        }
        last ->next = new_node;

        return head;
    }
};
Deleting a node in LinkedList
class Solution {
  public:
    void deleteNode(ListNode* node) {

        ListNode* ahead_node = node ->next;
        node ->val = ahead_node ->val;
        node ->next = ahead_node ->next;

    }
};
Find the length of the linkedlist
class Solution{
  public:
    int getCount(struct Node* head){

        int count = 0;
        Node* temp = head;
        while(temp != NULL){
            count++;
            temp = temp ->next;
        }

        return count;
    }
};
Search an element in the linkedlist
class Solution {
  public:
    bool searchKey(int n, struct Node* head, int key) {

        Node* temp = head;
        while(temp != NULL){
            if(temp ->data == key){
                return true;
            }
            temp = temp ->next;
        }
        return false;
    }
};
Deletion a node of given index
Node* delete_node(Node* head , int index){

    if(head == NULL){
        return NULL;
    }
    if(head != NULL){
        if(index == 1){
            head = head ->next;
            return head;
        }
    }

    Node* pre = NULL;
    Node* temp = head;
    int count = 0;
    while(temp != NULL){
        count++;
        if(count == index){
            pre ->next = temp ->next;
            free(pre ->next);
        }
        pre = temp;
        temp = temp ->next;
    }
    return head;
}
Insertion a node of given index
Node* insert_node(Node* head , int num , int idx){

    if(head == NULL){
        return NULL;
    }
    if(head != NULL){
        if(idx == 1){
            Node* temp = new Node(num);
            temp ->next = head;
            return temp;
        }
    }

    Node* pre = NULL;
    Node* temp = head;
    int count = 0;
    while(temp != NULL){
        count++;
        if(count == idx){
            Node* new_ele = new Node(num);
            pre ->next = new_ele;
            new_ele ->next = temp;
        }
        pre = temp;
        temp = temp ->next;
    } 
    return head;
}
Reverse a Linked List
class Solution {
  public:
    ListNode* reverseList(ListNode* head) {

        ListNode* curr = head;
        ListNode* pre = NULL;

        while(curr != NULL){
            ListNode* next = curr ->next;
            curr ->next = pre;
            pre = curr;
            curr = next;
        }

        return pre;
    }
};

🥇 🥇 🥇

Other Important Questions List

Important Questions List

💯 🔥 🚀