How do you add a node to the beginning of a linked list in Java?

Linked List | Set 2 [Inserting a node]

We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of linked list.

C++




// A linked list node
class Node
{
public:
int data;
Node *next;
};
// This code is contributed by rathbhupendra
C




// A linked list node
struct Node
{
int data;
struct Node *next;
};
Java




// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node[int d] {data = d; next = null; }
}
}
Python




# Node class
class Node:
# Function to initialize the node object
def __init__[self, data]:
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__[self]:
self.head = None
C#




/* Linked list Node*/
public class Node
{
public int data;
public Node next;
public Node[int d] {data = d; next = null; }
}
Javascript




// Linked List Class
var head; // head of list
/* Node Class */
class Node {
// Constructor to create a new node
constructor[d] {
this.data = d;
this.next = null;
}
}
// This code is contributed by todaysgaurav

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1] At the front of the linked list
2] After a given node.
3] At the end of the linked list.

Adding an Element to the Front of LinkedList in Java

A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements are linked using pointers and addresses. Each element is known as a node. This article shows how to add an element to the front of LinkedList in Java.

Method 1: [Using user-defined method]

  • Allocate the memory to a new node.
  • Put in the element to be inserted in the allocated node.
  • Make the next of the new node as the head.
  • Move the head to point to the new node.

Example:

Java




// Java program to Add an Element
// to the Front of LinkedList
import java.io.*;
class LinkedList {
// head reference
Node head;
// Node class
class Node {
int data;
Node next;
Node[int d]
{
data = d;
next = null;
}
}
// Inserting node at the front
public void insertfront[int data]
{
// Allocating and inserting the data in that node
Node new_node = new Node[data];
// Make the next of the newly allocated node to be
// the head
new_node.next = head;
// Now make the head to be the newly allocated node
head = new_node;
}
// Printing the List
public void print[]
{
Node temp = head;
while [temp != null] {
System.out.print[temp.data + " "];
temp = temp.next;
}
}
public static void main[String args[]]
{
// create a linkedlist
LinkedList l = new LinkedList[];
// insert elements at the front
l.insertfront[6];
l.insertfront[5];
l.insertfront[8];
l.insertfront[9];
// print the linkedlist
l.print[];
}
}
Output 9 8 5 6

Method 2: [Using addFirst[E e] method of LinkedList]



Declaration:

void addFirst[Object element]

Syntax:

LinkedList.addFirst[e]

Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended at beginning of the list.

Return Value: This method does not return any value.

Java




// Java program to Add an Element
// to the Front of LinkedList
import java.util.LinkedList;
class AddElementsAtTheFront {
public static void main[String args[]]
{
// create a LinkedList
LinkedList list = new LinkedList[];
// add elements at the front
list.addFirst["HI"];
list.addFirst["HOW"];
list.addFirst["ARE"];
list.addFirst["YOU"];
// print LinkedList
System.out.print[list];
}
}
Output [YOU, ARE, HOW, HI]

Method 3: [Using offerFirst[E e]]

This method also inserts the specified element at the front of the list.

Declaration:

public boolean offerFirst[E e]

Syntax:

LinkedList.offerFirst[e]

Parameters: Here, e is the element to add

Return Value: This method returns true

Example:

Java




// Java program to Add an Element
// to the Front of LinkedList
import java.util.LinkedList;
class AddingElementsAtTheFront {
public static void main[String args[]]
{
// create a LinkedList
LinkedList list = new LinkedList[];
// add elements at the front
list.offerFirst["HI"];
list.offerFirst["HOW"];
list.offerFirst["ARE"];
list.offerFirst["YOU"];
// print the LinkedList
System.out.print[list];
}
}
Output [YOU, ARE, HOW, HI]




Article Tags :
Java
Java Programs
Technical Scripter
java-LinkedList
Technical Scripter 2020
Practice Tags :
Java
Read Full Article

Inserting a new Node at the beginning of a LinkeList in Java

« Previous Next »

Java Program to insert a new node at the beginning of the singly linked list

In this Example, we will create a java program of singly linked list and add a new node at the beginning of the list.

Program: public class Main { class Node{ int data; Node next; public Node[int data] { this.data = data; this.next = null; } } public Node head = null; public Node tail = null; public void addAtStart[int data] { Node newNode = new Node[data]; if[head == null] { head = newNode; tail = newNode; } else { Node temp = head; head = newNode; head.next = temp; } } public void display[] { Node current = head; if[head == null] { System.out.println["List is empty"]; return; } System.out.println["Adding nodes to the start of the list: "]; while[current != null] { System.out.print[current.data + " "]; current = current.next; } System.out.println[]; } public static void main[String[] args] { Main sList = new Main[]; sList.addAtStart[1]; sList.display[]; sList.addAtStart[2]; sList.display[]; sList.addAtStart[3]; sList.display[]; sList.addAtStart[4]; sList.display[]; } }
Output:-
Adding nodes to the start of the list:
1
Adding nodes to the start of the list:
2 1
Adding nodes to the start of the list:
3 2 1
Adding nodes to the start of the list:
4 3 2 1
« Previous Next »

Inserting a node at the beginning of a linked list

The new node will be added at the beginning of a linked list.


Example

Assume that the linked list has elements: 20 30 40 NULL

If we insert 100, it will be added at the beginning of a linked list.

After insertion, the new linked list will be

100 20 30 40 NULL



Video liên quan

Bài mới nhất

Chủ Đề