Remove first node of the linked list
Last Updated :
22 Feb, 2024
Given a linked list, the task is to remove the first node of the linked list and update the head pointer of the linked list.Â
Examples:Â
Input : 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output : 2 -> 3 -> 4 -> 5 -> NULL
Input : 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output : 4 -> 6 -> 8 -> 33 -> 67 -> NULL
To remove the first node, we need to make the second node as head and delete the memory allocated for the first node. Â
Implementation:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* removeFirstNode( struct Node* head)
{
if (head == NULL)
return NULL;
Node* temp = head;
head = head->next;
delete temp;
return head;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
head = removeFirstNode(head);
for (Node* temp = head; temp != NULL; temp = temp->next)
cout << temp->data << " " ;
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node removeFirstNode(Node head)
{
if (head == null )
return null ;
Node temp = head;
head = head.next;
return head;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 12 );
head = push(head, 29 );
head = push(head, 11 );
head = push(head, 23 );
head = push(head, 8 );
head = removeFirstNode(head);
for (Node temp = head; temp != null ; temp = temp.next)
System.out.print(temp.data + " " );
}
}
|
Python3
import sys
class Node:
def __init__( self , data):
self .data = data
self . next = None
def removeFirstNode(head):
if not head:
return None
temp = head
head = head. next
temp = None
return head
def push(head, data):
if not head:
return Node(data)
temp = Node(data)
temp. next = head
head = temp
return head
if __name__ = = '__main__' :
head = None
head = push(head, 12 )
head = push(head, 29 )
head = push(head, 11 )
head = push(head, 23 )
head = push(head, 8 )
head = removeFirstNode(head)
while (head):
print ( "{} " . format (head.data), end = "")
head = head. next
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node removeFirstNode(Node head)
{
if (head == null )
return null ;
Node temp = head;
head = head.next;
return head;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
head = removeFirstNode(head);
for (Node temp = head; temp != null ; temp = temp.next)
Console.Write(temp.data + " " );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function removeFirstNode( head) {
if (head == null )
return null ;
temp = head;
head = head.next;
return head;
}
function push( head_ref , new_data) {
new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
head = removeFirstNode(head);
for ( temp = head; temp != null ; temp = temp.next)
document.write(temp.data + " " );
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...