{"id":1267,"date":"2022-02-26T07:13:10","date_gmt":"2022-02-26T07:13:10","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1267"},"modified":"2022-02-26T07:13:10","modified_gmt":"2022-02-26T07:13:10","slug":"linked-list","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/linked-list\/","title":{"rendered":"Linked List"},"content":{"rendered":"\n<p><br>Here is how we can create linked lists in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LinkedList&lt;Type&gt; linkedList = new LinkedList&lt;&gt;();<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>Type<\/var>&nbsp;indicates the type of a linked list. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create Integer type linked list\nLinkedList&lt;Integer&gt; linkedList = new LinkedList&lt;&gt;();\n\n\/\/ create String type linked list\nLinkedList&lt;String&gt; linkedList = new LinkedList&lt;&gt;();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Create LinkedList in Java<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n  public static void main(String&#91;] args){\n\n    \/\/ create linkedlist\n    LinkedList&lt;String&gt; animals = new LinkedList&lt;&gt;();\n\n    \/\/ Add elements to LinkedList\n    animals.add(\"Dog\");\n    animals.add(\"Cat\");\n    animals.add(\"Cow\");\n    System.out.println(\"LinkedList: \" + animals);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Dog, Cat, Cow]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a&nbsp;<code>LinkedList<\/code>&nbsp;named&nbsp;<var>animals<\/var>.<\/p>\n\n\n\n<p>Here, we have used the&nbsp;<code>add()<\/code>&nbsp;method to add elements to the LinkedList. We will learn more about the&nbsp;<code>add()<\/code>&nbsp;method later in this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Working of a Java LinkedList<\/h2>\n\n\n\n<p>Elements in linked lists are not stored in sequence. Instead, they are scattered and connected through links (<strong>Prev<\/strong>&nbsp;and&nbsp;<strong>Next<\/strong>).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-linkedlist-implementation.png\" alt=\"3 linkedlist nodes each connecting to one another using pointers\" title=\"Java LinkedList Implementation\"\/><figcaption>Java LinkedList Implementation<\/figcaption><\/figure>\n\n\n\n<p>Here we have 3 elements in a linked list.<\/p>\n\n\n\n<ul><li><var>Dog<\/var>&nbsp;&#8211; it is the first element that holds&nbsp;<var>null<\/var>&nbsp;as previous address and the address of&nbsp;<var>Cat<\/var>&nbsp;as the next address<\/li><li><var>Cat<\/var>&nbsp;&#8211; it is the second element that holds an address of&nbsp;<var>Dog<\/var>&nbsp;as the previous address and the address of&nbsp;<var>Cow<\/var>&nbsp;as the next address<\/li><li><var>Cow<\/var>&nbsp;&#8211; it is the last element that holds the address of&nbsp;<var>Cat<\/var>&nbsp;as the previous address and&nbsp;<var>null<\/var>&nbsp;as the next element<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of Java LinkedList<\/h2>\n\n\n\n<p><code>LinkedList<\/code>&nbsp;provides various methods that allow us to perform different operations in linked lists. We will look at four commonly used LinkedList Operators in this tutorial:<\/p>\n\n\n\n<ul><li>Add elements<\/li><li>Access elements<\/li><li>Change elements<\/li><li>Remove elements<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"add\">1. Add elements to a LinkedList<\/h3>\n\n\n\n<p>We can use the&nbsp;<code>add()<\/code>&nbsp;method to add an element (node) at the end of the LinkedList. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n  public static void main(String&#91;] args){\n    \/\/ create linkedlist\n    LinkedList&lt;String&gt; animals = new LinkedList&lt;&gt;();\n\n    \/\/ add() method without the index parameter\n    animals.add(\"Dog\");\n    animals.add(\"Cat\");\n    animals.add(\"Cow\");\n    System.out.println(\"LinkedList: \" + animals);\n\n    \/\/ add() method with the index parameter\n    animals.add(1, \"Horse\");\n    System.out.println(\"Updated LinkedList: \" + animals);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Dog, Cat, Cow]\nUpdated LinkedList: &#91;Dog, Horse, Cat, Cow]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a LinkedList named&nbsp;<var>animals<\/var>. Here, we have used the&nbsp;<code>add()<\/code>&nbsp;method to add elements to&nbsp;<var>animals<\/var>.<\/p>\n\n\n\n<p>Notice the statement,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>animals.add(1, \"Horse\");<\/code><\/pre>\n\n\n\n<p>Here, we have used the&nbsp;<strong>index number<\/strong>&nbsp;parameter. It is an optional parameter that specifies the position where the new element is added.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"get\">2. Access LinkedList elements<\/h3>\n\n\n\n<p>The&nbsp;<code>get()<\/code>&nbsp;method of the LinkedList class is used to access an element from the LinkedList. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    LinkedList&lt;String&gt; languages = new LinkedList&lt;&gt;();\n\n    \/\/ add elements in the linked list\n    languages.add(\"Python\");\n    languages.add(\"Java\");\n    languages.add(\"JavaScript\");\n    System.out.println(\"LinkedList: \" + languages);\n\n    \/\/ get the element from the linked list\n    String str = languages.get(1);\n    System.out.print(\"Element at index 1: \" + str);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Python, Java, JavaScript]\nElement at index 1: Java<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<code>get()<\/code>&nbsp;method with parameter&nbsp;<strong>1<\/strong>. Here, the method returns the element at index&nbsp;<strong>1<\/strong>.<\/p>\n\n\n\n<p>We can also access elements of the LinkedList using the&nbsp;<code>iterator()<\/code>&nbsp;and the&nbsp;<code>listIterator()<\/code>&nbsp;method. To learn more, visit the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/examples\/access-element-from-linkedlist\">Java program to access elements of LinkedList<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"change\">3. Change Elements of a LinkedList<\/h3>\n\n\n\n<p>The&nbsp;<code>set()<\/code>&nbsp;method of&nbsp;<code>LinkedList<\/code>&nbsp;class is used to change elements of the LinkedList. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    LinkedList&lt;String&gt; languages = new LinkedList&lt;&gt;();\n\n    \/\/ add elements in the linked list\n    languages.add(\"Java\");\n    languages.add(\"Python\");\n    languages.add(\"JavaScript\");\n    languages.add(\"Java\");\n    System.out.println(\"LinkedList: \" + languages);\n\n    \/\/ change elements at index 3\n    languages.set(3, \"Kotlin\");\n    System.out.println(\"Updated LinkedList: \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Java, Python, JavaScript, Java]\nUpdated LinkedList: &#91;Java, Python, JavaScript, Kotlin]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a LinkedList named languages. Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>languages.set(3, \"Kotlin\");<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>set()<\/code>&nbsp;method changes the element at index&nbsp;<strong>3<\/strong>&nbsp;to&nbsp;<var>Kotlin<\/var>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"remove\">4. Remove element from a LinkedList<\/h3>\n\n\n\n<p>The&nbsp;<code>remove()<\/code>&nbsp;method of the&nbsp;<code>LinkedList<\/code>&nbsp;class is used to remove an element from the LinkedList. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    LinkedList&lt;String&gt; languages = new LinkedList&lt;&gt;();\n\n    \/\/ add elements in LinkedList\n    languages.add(\"Java\");\n    languages.add(\"Python\");\n    languages.add(\"JavaScript\");\n    languages.add(\"Kotlin\");\n    System.out.println(\"LinkedList: \" + languages);\n\n    \/\/ remove elements from index 1\n    String str = languages.remove(1);\n    System.out.println(\"Removed Element: \" + str);\n\n    System.out.println(\"Updated LinkedList: \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Java, Python, JavaScript, Kotlin]\nRemoved Element: Python\nNew LinkedList: &#91;Java, JavaScript, Kotlin]<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>remove()<\/code>&nbsp;method takes the index number as the parameter. And, removes the element specified by the index number.<\/p>\n\n\n\n<p>To learn more about removing elements from the linkedlist, visit the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/examples\/remove-element-from-linkedlist\">Java program to remove elements from LinkedList.<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Other Methods<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Description<\/th><\/tr><tr><td><code>contains()<\/code><\/td><td>checks if the LinkedList contains the element<\/td><\/tr><tr><td><code>indexOf()<\/code><\/td><td>returns the index of the first occurrence of the element<\/td><\/tr><tr><td><code>lastIndexOf()<\/code><\/td><td>returns the index of the last occurrence of the element<\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>removes all the elements of the LinkedList<\/td><\/tr><tr><td><code>iterator()<\/code><\/td><td>returns an iterator to iterate over LinkedList<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"queue-deque\">LinkedList as Deque and Queue<\/h2>\n\n\n\n<p>Since the\u00a0<code>LinkedList<\/code>\u00a0class also implements the\u00a0Queue\u00a0and the\u00a0<a href=\"https:\/\/www.programiz.com\/java-programming\/deque\">Deque<\/a>\u00a0interface, it can implement methods of these interfaces as well. Here are some of the commonly used methods:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><code>addFirst()<\/code><\/td><td>adds the specified element at the beginning of the linked list<\/td><\/tr><tr><td><code>addLast()<\/code><\/td><td>adds the specified element at the end of the linked list<\/td><\/tr><tr><td><code>getFirst()<\/code><\/td><td>returns the first element<\/td><\/tr><tr><td><code>getLast()<\/code><\/td><td>returns the last element<\/td><\/tr><tr><td><code>removeFirst()<\/code><\/td><td>removes the first element<\/td><\/tr><tr><td><code>removeLast()<\/code><\/td><td>removes the last element<\/td><\/tr><tr><td><code>peek()<\/code><\/td><td>returns the first element (head) of the linked list<\/td><\/tr><tr><td><code>poll()<\/code><\/td><td>returns and removes the first element from the linked list<\/td><\/tr><tr><td><code>offer()<\/code><\/td><td>adds the specified element at the end of the linked list<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Java LinkedList as Queue<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\nimport java.util.Queue;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    Queue&lt;String&gt; languages = new LinkedList&lt;&gt;();\n\n    \/\/ add elements\n    languages.add(\"Python\");\n    languages.add(\"Java\");\n    languages.add(\"C\");\n    System.out.println(\"LinkedList: \" + languages);\n\n    \/\/ access the first element\n    String str1 = languages.peek();\n    System.out.println(\"Accessed Element: \" + str1);\n\n    \/\/ access and remove the first element\n    String str2 = languages.poll();\n    System.out.println(\"Removed Element: \" + str2);\n    System.out.println(\"LinkedList after poll(): \" + languages);\n\n    \/\/ add element at the end\n    languages.offer(\"Swift\");\n    System.out.println(\"LinkedList after offer(): \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Python, Java, C]\nAccessed Element: Python\nRemoved Element: Python\nLinkedList after poll(): &#91;Java, C]\nLinkedList after offer(): &#91;Java, C, Swift]<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example: LinkedList as Deque<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\nimport java.util.Deque;\n\nclass Main {\n  public static void main(String&#91;] args){\n    Deque&lt;String&gt; animals = new LinkedList&lt;&gt;();\n\n    \/\/ add element at the beginning\n    animals.add(\"Cow\");\n    System.out.println(\"LinkedList: \" + animals);\n\n    animals.addFirst(\"Dog\");\n    System.out.println(\"LinkedList after addFirst(): \" + animals);\n\n    \/\/ add elements at the end\n    animals.addLast(\"Zebra\");\n    System.out.println(\"LinkedList after addLast(): \" + animals);\n\n    \/\/ remove the first element\n    animals.removeFirst();\n    System.out.println(\"LinkedList after removeFirst(): \" + animals);\n\n    \/\/ remove the last element\n    animals.removeLast();\n    System.out.println(\"LinkedList after removeLast(): \" + animals);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Cow]\nLinkedList after addFirst(): &#91;Dog, Cow]\nLinkedList after addLast(): &#91;Dog, Cow, Zebra]\nLinkedList after removeFirst(): &#91;Cow, Zebra]\nLinkedList after removeLast(): &#91;Cow]<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"iterate\">Iterating through LinkedList<\/h2>\n\n\n\n<p>We can use the\u00a0Java for-each loop\u00a0to iterate through LinkedList. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        \/\/ Creating a linked list\n        LinkedList&lt;String&gt; animals = new LinkedList&lt;&gt;();\n        animals.add(\"Cow\");\n        animals.add(\"Cat\");\n        animals.add(\"Dog\");\n        System.out.println(\"LinkedList: \" + animals);\n\n        \/\/ Using forEach loop\n        System.out.println(\"Accessing linked list elements:\");\n        for(String animal: animals) {\n            System.out.print(animal);\n            System.out.print(\", \");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>LinkedList: &#91;Cow, Cat, Dog]\nAccessing linked list elements:\nCow, Cat, Dog,<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"linkedlist-vs-arraylist\">LinkedList Vs. ArrayList<\/h2>\n\n\n\n<p>Both the\u00a0Java ArrayList\u00a0and\u00a0<code>LinkedList<\/code>\u00a0implements the\u00a0<code>List<\/code>\u00a0interface of the\u00a0<code>Collections<\/code>\u00a0framework. However, there exists some difference between them.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>LinkedList<\/th><th>ArrayList<\/th><\/tr><tr><td>Implements <code>List<\/code>, <code>Queue<\/code>, and <code>Deque<\/code> interfaces.<\/td><td>Implements <code>List<\/code> interface.<\/td><\/tr><tr><td>Stores 3 values (<strong>previous address<\/strong>, <strong>data,<\/strong> and <strong>next address<\/strong>) in a single position.<\/td><td>Stores a single value in a single position.<\/td><\/tr><tr><td>Provides the doubly-linked list implementation.<\/td><td>Provides a resizable array implementation.<\/td><\/tr><tr><td>Whenever an element is added, <code>prev<\/code> and <code>next<\/code> address are changed.<\/td><td>Whenever an element is added, all elements after that position are shifted.<\/td><\/tr><tr><td>To access an element, we need to iterate from the beginning to the element.<\/td><td>Can randomly access elements using indexes.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Note<\/strong>: We can also create a LinkedList using interfaces in Java. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create linkedlist using List\nList&lt;String&gt; animals1 = new LinkedList&lt;&gt;();\n\n\/\/ creating linkedlist using Queue\nQueue&lt;String&gt; animals2 = new LinkedList&lt;&gt;();\n\n\/\/ creating linkedlist using Deque\nDeque&lt;String&gt; animals3 = new LinkedList&lt;&gt;();<\/code><\/pre>\n\n\n\n<p>Here, if the LinkedList is created using one interface, then we cannot use methods provided by other interfaces. That is,&nbsp;<var>animals1<\/var>&nbsp;cannot use methods specific to&nbsp;<code>Queue<\/code>&nbsp;and&nbsp;<code>Deque<\/code>&nbsp;interfaces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is how we can create linked lists in Java: Here,&nbsp;Type&nbsp;indicates the type of a linked list. For example, Example: Create LinkedList in Java Output In the above example, we have created a&nbsp;LinkedList&nbsp;named&nbsp;animals. Here, we have used the&nbsp;add()&nbsp;method to add elements to the LinkedList. We will learn more about the&nbsp;add()&nbsp;method later in this tutorial. Working [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[291],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1267"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1267"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1267\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}