LinkedList add to front java

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 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 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<String> list = new LinkedList<String>();
// 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 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<String> list = new LinkedList<String>();
// 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]

LinkedList add to front java




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

Add elements at beginning and end of LinkedList in Java

Java 8Object Oriented ProgrammingProgramming

Elements can be added at the beginning of a LinkedList by using the method java.util.LinkedList.addFirst(). This method has a single argument i.e. the element that is to be added at the beginning of the LinkedList.

Elements can be added at the end of a LinkedList by using the method java.util.LinkedList.addLast(). This method has a single argument i.e. the element that is to be added at the end of the LinkedList.

A program that demonstrates this is given as follows −

Java Program to add elements at start and end of the linked list

Here is my Java solution to add an element at the head and tail position of a doubly linked list in Java. Why doubly linked list? Isn't we are using theLinkedList class from java.util package? Yes, that implementation of a doubly linked list.

This is not the pure logic solution, instead, we are using the API method to solve the problem. Java's LinkedList class provides addFirst() and addLast() methods to add an element at the start and end of the linked list. You can use them when your program requires adding messages at those positions.

You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about these useful collection classes in Java.

LinkedList add to front java


Now, here is our Java program to show how to add elements at the start and end of a linked list using the addFirst() and addLast() method, remember it's a doubly linked list.

import java.util.LinkedList; /** * Java Program to add elements at start and end of linked list * in Java. You can use addFirst() and addLast() method to * add an element at first and last position of linked list * in Java. * * @author java67 */ public class StringRotateDemo { public static void main(String args[]) { // Creating a linked list of numbers in String format LinkedList<String> listOfNumbers = new LinkedList<>(); listOfNumbers.add("100"); listOfNumbers.add("200"); listOfNumbers.add("300"); listOfNumbers.add("400"); listOfNumbers.add("500"); // let's print the linked list before adding new number System.out.println("Original linked list : "); System.out.println(listOfNumbers); // let's add an element at start of the linked list listOfNumbers.addFirst("000"); // now let's print the linked list again, 000 should // be at first position System.out.println("linked list after adding an element at start : "); System.out.println(listOfNumbers); // now let's add element at the end of the linked list // in Java listOfNumbers.addLast("600"); // let's print the linked list again, 600 should be // the last element System.out.println("linked list after adding an element at end : "); System.out.println(listOfNumbers); } } Output Original linked list : [100, 200, 300, 400, 500] linked list after adding an element at start : [000, 100, 200, 300, 400, 500] linked list after adding an element at end : [000, 100, 200, 300, 400, 500, 600]
That's all about how to add an element at the first and last position of the linked list in Java. You can use addFirst() and addLast() methods to add an element at the start and end of the list. Remember, the start of the linked list is also known as the head, and end of the linked list is known as the tail, so this solution also applicable when you need to add an element at the head and tail of a linked list in Java.

If you like this tutorial and interested in learning more about different classes of the Java Collection framework, you can also check out the following articles :
  • How to remove elements from ArrayList in Java? (example)
  • How to get a first and last element from ArrayList in Java? (example)
  • How to synchronized ArrayList in Java? (example)
  • How to reverse order of elements in ArrayList? (example)
  • How to remove duplicate elements from ArrayList in Java? (example)
  • How to create and initialize ArrayList in the same line? (example)
  • How to loop over ArrayList in Java? (example)
  • How to get sublist from ArrayList in Java? (example)
  • How to convert ArrayList to String in Java? (example)
  • How to make read only ArrayList in Java? (example)