Like arrays, Linked Listing is a linear information construction. In contrast to arrays, linked record components are usually not saved at a contiguous location; the weather are linked utilizing pointers. They embody a collection of related nodes. Right here, every node shops the information and the handle of the following node.
To study extra about linked record consult with the article “Introduction to Linked LIst“.
Given a linked record, the duty is to insert a brand new node originally of the linked record.
Insert Node on the entrance of a linked record
Instance:
Listing = 2->3->4->5, Insert a node with worth 1 at starting.
Output record shall be: 1->2->3->4->5
Think about the next representations of the linked record.Â
C++
class Node {
public :
    int information;
    Node* subsequent;
};
|
C
struct Node {
    int information;
    struct Node* subsequent;
};
|
Java
class LinkedList {
    Node head;
 Â
   Â
    class Node {
        int information;
        Node subsequent;
 Â
       Â
        Node( int d)
        {
            information = d;
            subsequent = null ;
        }
    }
}
|
Python3
class Node:
 Â
   Â
    def __init__( self , information):
        self .information = informationÂ
        self . subsequent = None Â
 Â
 Â
 Â
class LinkedList:
 Â
   Â
    def __init__( self ):
        self .head = None
|
C#
public class Node {
    public int information;
    public Node subsequent;
    public Node( int d)
    {
        information = d;
        subsequent = null ;
    }
}
|
Javascript
<script>
    var head;
 Â
   Â
    class Node {
 Â
       Â
        constructor(d) {
            this .information = d;
            this .subsequent = null ;
        }
    }
</script>
|
Strategy: The beneath steps needs to be adopted to insert a brand new node on the entrance of the linked record
- Allocate a brand new node (say temp).
- Put the required information into temp.
- The ‘subsequent’ pointer of the node needs to be pointed to the present head.
- Now make the head pointer level to temp.
See the beneath picture for a greater unedrstanding:
Insert a brand new node at entrance of Linked Listing
Under is the implementation of the strategy:
C++
void push(Node** head_ref, int new_data)
{
 Â
   Â
    Node* new_node = new Node();
 Â
   Â
    new_node->information = new_data;
 Â
   Â
    new_node->subsequent = (*head_ref);
 Â
   Â
   Â
    (*head_ref) = new_node;
}
|
C
  Â
  Â
Â
void push( struct Node** head_ref, int new_data)
{
   Â
    struct Node* new_node
        = ( struct Node*) malloc ( sizeof ( struct Node));
 Â
   Â
    new_node->information = new_data;
 Â
   Â
    new_node->subsequent = (*head_ref);
 Â
   Â
    (*head_ref) = new_node;
}
|
Java
  Â
  Â
public void push( int new_data)
{
   Â
             Â
    Node new_node = new Node(new_data);
 Â
   Â
    new_node.subsequent = head;
 Â
   Â
    head = new_node;
}
|
Python3
def push( self , new_data):
 Â
   Â
   Â
    new_node = Node(new_data)
 Â
   Â
    new_node. subsequent = self .head
 Â
   Â
    self .head = new_node
|
C#
public void push( int new_data)
{
   Â
              Â
    Node new_node = new Node(new_data);
 Â
   Â
    new_node.subsequent = head;
 Â
   Â
    head = new_node;
}
|
Javascript
<script>
 Â
  Â
  Â
    Â
 perform push(new_data)
{
   Â
             Â
    var new_node = new Node(new_data);
 Â
   Â
    new_node.subsequent = head;
 Â
   Â
    head = new_node;
}
 Â
 Â
</script>
|
Time Complexity: O(1)
Auxiliary House: O(1)