1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Insert an Element into LinkedList Java example This java example shows how to insert an element to specified index using add method of Java LinkedList class. */ import java.util.LinkedList; public class LinkedListInsertElementExample { public static void main(String[] args) { //create LinkedList object LinkedList lList = new LinkedList(); //add elements to LinkedList lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); System.out.println("LinkedList contains : " + lList); /* * To insert an element into LinkedList object, use * void add(int index , Object element) method. * * It inserts specified element at specified index in the LinkedList by * shifting current elements and subsequent elements to the right. */ lList.add(2, "2.5"); System.out.println("After inserting 2.5, LinkedList contains : " + lList); } } /* Output would be LinkedList contains : [1, 2, 3, 4, 5] After inserting 2.5, LinkedList contains : [1, 2, 2.5, 3, 4, 5] */ |
can u mail me the logic for the following example
find a particular element in the singly linked list & replace it with another element