# Insert node at beginning of linked list

In this post we are discussing about that how to created a linked list node. And how append this node at beginning of the linked list. That is interesting and because that are commonly asked question for implement heap and queue data structure using of linked list.

If you are know about how to make a linked list node and also know about how to append of this node in specific position. then it will helpful to solve complex problem of linked list in very effective manner.

Implementation process

1) First step create a dynamic linked list node and assign data and next pointer field value (when Assume that there are no memory overflow problem).

2) When linked list are empty so in this situation assign the reference of this create node to head pointer of linked list.

3) When linked list are not empty then do first steps. After that this created node pointer field are assigned the address of front node of linked list. And head pointer is assigned the address of newly create node.

This third step are work on most of cases. and time complexity of this process are O(1) constant. Suppose we are inserted the following (8, 7, 6, 5, 4, 3, 2, 1) node in a sequence.

Note that last inserted node is add first position. Here given code implementation process.

``````/*
C program for
*/
#include <stdio.h>
#include <stdlib.h>

{
int data;
};
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *)
malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
}
return sll;
}
// Handles the request of adding new node at beginning of linked list
void addNode(struct SingleLL *sll, int data)
{
// Create dynamic node
if (node == NULL)
{
return;
}
else
{
// Set initial node value
node->data = data;
}
}
{
if (node == NULL)
{
return;
}
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
int main(int argc, char const *argv[])
{
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
return 0;
}``````

#### Output

`````` Linked List
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``````
``````/*
Java program for
Insert node at beginning of linked list
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
}
{
// Create new node
// Connect current node to previous head
}
public void display()
{
{
return;
}
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Insert node at beginning of linked list
*/
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
SingleLL()
{
}
{
// Create new node
// Connect current node to previous head
}
void display()
{
{
return;
}
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
};
int main()
{
SingleLL *sll = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll->display();
return 0;
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````package main
import "fmt"
/*
Go program for
Insert node at beginning of linked list
*/
data int
}
me.data = data
me.next = nil
return me
}
type SingleLL struct {
}
func getSingleLL() * SingleLL {
var me *SingleLL = &SingleLL {}
return me
}
// Create new node
// Connect current node to previous head
}
func(this SingleLL) display() {
return
}
for (temp != nil) {
fmt.Print(" ", temp.data, " →")
// Visit to next node
temp = temp.next
}
fmt.Print(" NULL\n")
}
func main() {
var sll * SingleLL = getSingleLL()
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display()
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````// Include namespace system
using System;
/*
Csharp program for
Insert node at beginning of linked list
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
}
{
// Create new node
// Connect current node to previous head
}
public void display()
{
{
return;
}
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````<?php
/*
Php program for
Insert node at beginning of linked list
*/
{
public \$data;
public \$next;
public  function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
class SingleLL
{
public  function __construct()
{
}
{
// Create new node
// Connect current node to previous head
}
public  function display()
{
{
return;
}
while (\$temp != NULL)
{
echo(" ".\$temp->data.
" →");
// Visit to next node
\$temp = \$temp->next;
}
echo(" NULL\n");
}
}

function main()
{
\$sll = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
\$sll->display();
}
main();``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````/*
Node JS program for
Insert node at beginning of linked list
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
}
{
// Create new node
// Connect current node to previous head
}
display()
{
{
return;
}
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
}

function main()
{
var sll = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}
main();``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````#    Python 3 program for
#    Insert node at beginning of linked list

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :
def __init__(self) :

#  Create new node
#  Connect current node to previous head

def display(self) :
return

while (temp != None) :
print("", temp.data ,"→", end = "")
#  Visit to next node
temp = temp.next

print(" NULL")

def main() :
sll = SingleLL()
#  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display()

if __name__ == "__main__": main()``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````#    Ruby program for
#    Insert node at beginning of linked list

attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL
def initialize()
end

#  Create new node
#  Connect current node to previous head
end

def display()
return
end

while (temp != nil)
print(" ", temp.data ," →")
#  Visit to next node
temp = temp.next
end

print(" NULL\n")
end

end

def main()
sll = SingleLL.new()
#  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display()
end

main()``````

#### Output

`````` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
``````
``````/*
Scala program for
Insert node at beginning of linked list
*/
{
def this(data: Int)
{
this(data, null);
}
}
{
def this()
{
this(null);
}
def addNode(data: Int): Unit = {
// Create new node
// Connect current node to previous head
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````/*
Swift 4 program for
Insert node at beginning of linked list
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
init()
{
}
{
// Create new node
// Connect current node to previous head
}
func display()
{
{
return;
}
while (temp  != nil)
{
print("", temp!.data ,"→", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
}
func main()
{
let sll: SingleLL = SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}
main();``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``
``````/*
Kotlin program for
Insert node at beginning of linked list
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
}
{
// Create new node
// Connect current node to previous head
}
fun display(): Unit
{
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
}
fun main(args: Array < String > ): Unit
{
val sll: SingleLL = SingleLL();
// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
sll.display();
}``````

#### Output

`` 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL``

Time complexity of this program is O(1). When include new node firstly set new node pointer field is to head pointer. Firstly head linked list pointer is null, so newly create first node next pointer is also null. That is very useful to checking the end of linked list. After that include other node in similar manner.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.