Java Program to Remove elements from the LinkedList

JavaCampus InterviewServer Side ProgrammingProgramming

<p>In this article, we will understand how to remove elements from the linked-list.</p><p>The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.</p><p>Below is a demonstration of the same &minus;</p><p><strong>Suppose our input is</strong> &minus;</p><pre class="result notranslate">The list is defined as: [Java, Scala, Python, JavaScript, C++]</pre><p><strong>The desired output would be</strong> &minus;</p><pre class="result notranslate">The list after removing all the elements is: [Python, JavaScript, C++]</pre><h2>Algorithm</h2><pre class="result notranslate">Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Display the result Step 5 - Stop</pre><h2>Example 1</h2><p>Here, we bind all the operations together under the &lsquo;main&rsquo; function.</p><pre class="demo-code notranslate language-java" data-lang="java">import java.util.LinkedList; public class Demo { &nbsp; &nbsp;public static void main(String args[]){ &nbsp; &nbsp; &nbsp; LinkedList&lt;String&gt; input_list = new LinkedList&lt;String&gt;(); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Java&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Scala&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Python&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;JavaScript&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;C++&quot;); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The list is defined as: &quot; + input_list); &nbsp; &nbsp; &nbsp; input_list.remove(); &nbsp; &nbsp; &nbsp; input_list.remove(); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The list after removing all the elements is: &quot; + input_list); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]</pre><h2>Example 2</h2><p>Here, we encapsulate the operations into functions exhibiting object oriented programming.</p><pre class="demo-code notranslate language-java" data-lang="java">import java.util.LinkedList; public class Demo { &nbsp; &nbsp;static void remove_element(LinkedList&lt;String&gt; input_list){ &nbsp; &nbsp; &nbsp; input_list.remove(); &nbsp; &nbsp; &nbsp; input_list.remove(); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The list after removing all the elements is: &quot; + input_list); &nbsp; &nbsp;} &nbsp; &nbsp;public static void main(String args[]){ &nbsp; &nbsp; &nbsp; LinkedList&lt;String&gt; input_list = new LinkedList&lt;String&gt;(); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Java&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Scala&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;Python&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;JavaScript&quot;); &nbsp; &nbsp; &nbsp; input_list.add(&quot;C++&quot;); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The list is defined as: &quot; + input_list); &nbsp; &nbsp; &nbsp; remove_element(input_list); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]</pre>
raja
Updated on 29-Mar-2022 12:48:35

Advertisements