Adding an Element to the Front of LinkedList in JavaA 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. Show Method 1: (Using user-defined method)
Example: Java
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
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
Output
[YOU, ARE, HOW, HI]
Article Tags :
Java Java Programs Technical Scripter
java-LinkedList Technical Scripter 2020 Practice Tags :
Java Add elements at beginning and end of LinkedList in JavaJava 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 listHere 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. 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 :
|