<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Palistha Singh - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Palistha Singh - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 23 May 2026 08:50:01 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/palistha/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How Does Recursion Work? Explained with Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ In this article, you will learn about recursion and how it works. You need a good understanding of how functions work before learning recursion. I have used Python code for examples in this article because of its simple syntax, but the concept of rec... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-recursion/</link>
                <guid isPermaLink="false">66d4608b230dff016690584b</guid>
                
                    <category>
                        <![CDATA[ beginner ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Recursion ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Palistha Singh ]]>
                </dc:creator>
                <pubDate>Thu, 25 Jul 2024 15:03:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/Frame-1--6-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, you will learn about recursion and how it works.</p>
<p>You need a good understanding of how functions work before learning recursion. I have used Python code for examples in this article because of its simple syntax, but the concept of recursion is the same for every programming language.</p>
<h2 id="heading-what-is-recursion">What is Recursion?</h2>
<p>In most programming languages, a function can call another function. But a function can also call itself. Recursion is the technique where a function calls itself.</p>
<p>Here's an example:</p>
<pre><code class="lang-bash">def call_me():
    call_me()
</code></pre>
<p>Here, the function calls itself, which is called recursion.</p>
<p>But "calling itself" is just a programmatic definition of recursion. Recursion involves breaking down a problem into smaller pieces to the point that it cannot be further broken down. You solve the small pieces and put them together to solve the overall problem.</p>
<h2 id="heading-real-life-analogy-of-recursion">Real Life Analogy of Recursion</h2>
<p>Lets understand how recursion really works with the help of an example.</p>
<p>Imagine you're in line for a Disney ride, and you don't know how many people are ahead of you.</p>
<p>To find out, you ask the person in front of you.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/How-many--4-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Trying to find out how many people are in front of you in line</em></p>
<p>That person also doesn't know, so they ask the person in front of them.</p>
<p>This process continues until the question reaches the person at the very front of the line, who sees that there is no one in front of them and replies that there are zero people ahead.</p>
<p>The replies then start to propagate back through the line. Each person adds one to the number they were told before passing the information back.</p>
<p>When the person at the front replies, <strong>"There are 0 people ahead"</strong> the next person adds one and replies, <strong>"There is 1 person ahead"</strong> and so on.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/How-many--5-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Everyone knows how many people are in front of them in line</em></p>
<p>By the time the response reaches the person directly in front of you, they add one more and tell you. This way, you can determine your position in the line by just adding <strong>1</strong> to the number the person in front of you gave.</p>
<p>This example illustrates how recursion breaks a problem into smaller subproblems and then combines their solutions to solve the original problem.</p>
<p>Each person in the line represents a smaller instance of the same problem: determining the number of people ahead. By solving these smaller instances and combining their results, the overall problem is resolved. This is exactly how recursion works.</p>
<h2 id="heading-technical-details-of-recursion">Technical Details of Recursion</h2>
<p>The most important things to consider while coding recursion is to find out:</p>
<ul>
<li><p><strong>Recursive Case:</strong> Minimum work we can do. In the above example, asking the person in front of you how many people are ahead of them is the least amount of work we can do.</p>
</li>
<li><p><strong>Base Case:</strong> Condition where no work is required. In the above example, the person on the front of the line doesn’t have to ask anything so it is the condition where no work is required.</p>
</li>
</ul>
<h2 id="heading-simple-example-of-recursion">Simple Example of Recursion</h2>
<p>Calculating a factorial is the simplest example of recursion that will really help you understand how it works.</p>
<p>There are many ways to calculate the factorial of a number. But here we will see the recursive way to find it.</p>
<p>Before thinking about how we do it, we need to know what the factorial of a number is.</p>
<p>The factorial of a number is the multiplication of all numbers from <strong>1</strong> up to that number.</p>
<p>For example, the factorial of <strong>5</strong> is <strong>120</strong> – that is <strong>5</strong>×<strong>4</strong>×<strong>3</strong>×<strong>2</strong>×<strong>1</strong>.</p>
<p>We can also represent this mathematically like this:</p>
<p><code>5×(5−1)!</code></p>
<p>This means that if we know the value of <code>(5−1)!</code> we can easily get the factorial by just multiplying <strong>5</strong> by it.</p>
<p>Here’s how we find the factorials of <strong>4</strong>, <strong>3</strong>, <strong>2</strong>, <strong>1</strong>, and <strong>0</strong>:</p>
<pre><code class="lang-bash">Factorial of 4 = 4×(4−1)!
Factorial of 3 = 3×(3−1)!
Factorial of 2 = 2×(2−1)!
Factorial of 1 = 1
Factorial of 0 = 1
</code></pre>
<p>By looking at this, it is clear that to find the factorial of <strong>5</strong>, we must multiply <strong>5</strong> by <code>4!</code>.</p>
<h3 id="heading-more-general-example">More general example</h3>
<p>To find the factorial of <code>n</code>, we need to multiply <code>n</code> with <code>(n−1)!</code>. This is something you need to do recursively.</p>
<p>Now, there must be a stopping condition for recursion. The stopping condition is where we perform no further operation. When <code>n</code> is <strong>1</strong> or <code>n</code> is <strong>0</strong>, we can simply stop the recursion as these values have known factorials. We can simply say the factorial of <strong>1</strong> is <strong>1</strong> and the same is true for <strong>0</strong>.</p>
<p>So, breaking it down, the minimum amount of work we need to do to find the factorial of n is <code>n×(n−1)!</code>. And we can stop performing operations on it when we find the factorial of <strong>1</strong> or <strong>0</strong>.</p>
<p>Let's see how it looks in code:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># calculate factorial of n</span>
def fact(n):

   <span class="hljs-comment"># no work required</span>
    <span class="hljs-keyword">if</span> n == 1 or n == 0:
        <span class="hljs-built_in">return</span> 1

    <span class="hljs-comment"># minimum amount of work</span>
    <span class="hljs-built_in">return</span> n * fact(n - 1)

n = 5

<span class="hljs-comment"># calculate factorial</span>
factorial = fact(n)
<span class="hljs-built_in">print</span>(factorial)
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-bash">120
</code></pre>
<p>Let's see how it works:</p>
<p>In the first function call, the factorial of <strong>5</strong> is evaluated. Then in the second call, the factorial of <strong>4</strong> is evaluated, and so on until the factorial of <strong>2</strong> is evaluated.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-1--5-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Recursively calculating Factorial of 5</em></p>
<p>While calling the factorial of <strong>2</strong>, we have <code>2×fact(2−1)</code>, which is <code>2×fact(1)</code>.</p>
<p>This hits our base case. So, the recursion stops and <code>2×fact(1)</code> returns <code>2×1</code> to the preceding function call, and the result gets popped up the stack.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Fourth function call returning 2 to preceding function call and getting popped up from the stack</em></p>
<p>Similarly, here’s how everything else evaluates:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>3rd function call returning 6 to the preceding function call and getting popped up from the stack</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-5.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>2nd function call returning 24 to the preceding function call and getting popped up from the stack</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>1st function call returning 120 to the initial function call and getting popped up from the stack</em></p>
<p>So the function finally returns the value <strong>120</strong> to the initial function call.</p>
<h3 id="heading-why-do-we-need-a-base-case">Why Do We Need a Base Case?</h3>
<p>In the above example we have used the stopping condition for the code. But what if we don’t add a stopping condition or what if the function we write never meets the stopping condition?</p>
<p>Will the code run forever?</p>
<p>No – even if you don’t terminate, your code won't run forever. Let’s understand why this is the case with the help of an example.</p>
<pre><code class="lang-bash">def print_five():
    <span class="hljs-built_in">print</span>(5)

    <span class="hljs-comment"># call itself</span>
    print_five()

<span class="hljs-comment"># function call</span>
print_five()
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-bash">5
5
5
...

RecursionError: maximum recursion depth exceeded
</code></pre>
<p>If you run the above code, you will see that the function doesn’t run forever and ends with a message <code>RecursionError: maximum recursion depth exceeded</code>.</p>
<p>When a function is invoked, it is stored in a call stack. Here's how the function <code>print_five()</code> is stored in the call stack when it is invoked for the first time.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/How-many--6-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Call stack on the first function call</em></p>
<p>The function calls itself again and again, and the function is stored in the call stack with each call.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/How-many--8-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Call stack after n function calls</em></p>
<p>But the call stack has a limited size and cannot store an unlimited number of functions.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/How-many--7-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>No space on call stack resulting in stack overflow</em></p>
<p>When the stack is full, it cannot accommodate any more calls, causing a stack overflow error.</p>
<p>Therefore, the base case is essential to prevent such errors and ensure the recursion terminates properly.</p>
<p>Now let's see another example to understand recursion even more.</p>
<h2 id="heading-how-to-check-if-a-word-is-a-palindrome">How to Check if a Word is a Palindrome</h2>
<p>Before we dive into the code, you should know what a palindrome is. A palindrome is a word that reads the same forwards and backwards.</p>
<p>For example, <code>racecar</code> reads the same forwards and backwards.</p>
<p>To check if a word is a palindrome, we have to check if the first and last letters are the same. If they are, we then check if the second and second-to-last letters are the same.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Check if first and last characters of racecar are the same</em></p>
<p>In the context of <code>racecar</code>, the first and last letters are the same, so we check if the second and second-to-last letters are the same. They are, so now we check if the third and third-to-last letters are the same. Now there is only one letter left to check. A single letter is always a palindrome because it reads the same both ways.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>How to check if racecar is a palindrome</em></p>
<p>So, now let's try thinking about it recursively, which involves the minimum amount of work and determining when no work is required.</p>
<h3 id="heading-minimum-amount-of-work">Minimum amount of work</h3>
<p>Check if the first and last letters are the same, and if they are, remove the first and last letters from the word.</p>
<h3 id="heading-no-work-required">No work required</h3>
<p>When there is one letter or no letters left at all, we can simply say it is a palindrome.</p>
<p>Now, let's see how the code looks:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># check palindrome</span>
def check_palindrome(text):

    <span class="hljs-comment"># stopping condition</span>
    <span class="hljs-comment"># if the size of text is 1 or 0 return true</span>
    <span class="hljs-keyword">if</span> len(text) == 0 or len(text) == 1:
        <span class="hljs-built_in">return</span> True

    <span class="hljs-comment"># least amount of work</span>
    <span class="hljs-comment"># check if first and last char are the same</span>
    <span class="hljs-comment"># if same remove first and last char from string</span>
    <span class="hljs-keyword">if</span>(text[0]==text[-1]):
        <span class="hljs-built_in">return</span>(check_palindrome(text[1:-1]))

    <span class="hljs-built_in">return</span> False

<span class="hljs-comment"># check if string is palindrome</span>
text = <span class="hljs-string">"racecar"</span>
is_palindrome = check_palindrome(text)
<span class="hljs-built_in">print</span>(is_palindrome)
</code></pre>
<p>Here's how the above code works:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/Frame-9.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Check if racecar is a palindrome</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/07/checkpalindrome.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Check if racecar is a palindrome</em></p>
<h2 id="heading-when-to-use-recursion">When to Use Recursion</h2>
<p>Recursion can appear elegant and simple. But it often requires many steps to solve even simple problems due to the CPU overhead from repeatedly adding methods to the stack. So before using it, make sure you carefully consider whether it's the right solution for your problem.</p>
<p>When code requires multiple loops and looks confusing and messy, recursion can offer a cleaner solution. Its use, however, depends on the specific code and the type of data or data structure involved. For data structures like trees and graphs, recursion can be particularly useful.</p>
<p>Despite its simplicity in appearance, recursion can be hard to understand and may take multiple steps even for simple problems. So again, make sure you think about your particular use case.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>This is just an introduction to recursion. There are many cases where recursion is used, and you might be confused about how everything works. I will cover more advanced examples on recursion in the next article.</p>
<p>By the way, here are the resources that I found simple and useful while learning recursion:</p>
<ul>
<li><p><a target="_blank" href="https://www.youtube.com/watch?v=IJDJ0kBx2LM&amp;t=657s">freeCodeCamp video on recursion</a>: I have to give a shout-out to freeCodeCamp for their excellent video on recursion, which inspired much of this article.</p>
</li>
<li><p><a target="_blank" href="https://programiz.pro/course/learn-recursion-with-python">Recursion by Programiz Pro</a>: Another good resource is the Recursion course by Programiz. It's a premium course, so it's not free, but it's thoughtfully designed. Plus, you can actually practice directly on their platform, which makes it well worth it.</p>
</li>
</ul>
<p>No matter where you learn from, don't spend too much time searching for the perfect resource. Just grasp the concepts and start practicing—that's the only way you'll truly learn.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Does a Linked List Work? A Beginner's Guide to Linked Lists ]]>
                </title>
                <description>
                    <![CDATA[ A Linked List is a linear data structure used for storing a collection of elements. Unlike arrays, linked lists use nodes to store elements which are not stored in contiguous memory locations. In this article, you will learn what linked lists are, ho... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-linked-lists-work/</link>
                <guid isPermaLink="false">66d4608a8812486a37369d34</guid>
                
                    <category>
                        <![CDATA[ data structures ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Palistha Singh ]]>
                </dc:creator>
                <pubDate>Fri, 12 May 2023 17:45:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/pexels-joey-kyber-119562.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A Linked List is a linear data structure used for storing a collection of elements. Unlike arrays, linked lists use nodes to store elements which are not stored in contiguous memory locations.</p>
<p>In this article, you will learn what linked lists are, how they work, and how to build one.</p>
<p>While the concepts discussed are not specific to any particular programming language, this article will use Java to demonstrate how to create a linked list programmatically.</p>
<h2 id="heading-what-is-a-linked-list">What is a Linked List?</h2>
<p>A linked list is a collection of nodes where each node contains data as well as the memory address of the next node in the list.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration of a linked list with three nodes</em></p>
<p>Here, you can see that the addresses of the nodes are not necessarily immediately sequential. The first node has an address of <strong>200</strong> and the second node has an address of <strong>801</strong>, instead of <strong>201</strong> as you might expect.</p>
<p>Then how are the nodes stored linearly?</p>
<p>Even though the nodes are not in a contiguous memory, the nodes are stored linearly through links. Every node has the address of its succeeding node. That is how each node can access its succeeding node.</p>
<h2 id="heading-nodes-in-a-linked-list">Nodes in a Linked List</h2>
<p>Nodes are the building block of the linked list. After all, a linked list is a collection of nodes.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Example node</em></p>
<p>A node in a linked list consists of two parts:</p>
<ul>
<li><p><code>data</code> which denotes the value of the node.</p>
</li>
<li><p><code>next</code> which is a reference to the succeeding node.</p>
</li>
</ul>
<h2 id="heading-head-and-tail-in-a-linked-list">Head and Tail in a Linked List</h2>
<p>As mentioned earlier, a linked list is a collection of nodes.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/9.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration of a linked list showing the head and tail</em></p>
<p>The first node of the linked list is called the <code>head</code> node. It is the starting point of a linked list.</p>
<p>The last node is called the <code>tail</code> node. As there is no node after the last node, the last node always points to the <code>null</code>.</p>
<p>A <code>null</code> pointer does not point to any memory location.</p>
<h2 id="heading-how-to-create-a-linked-list-programmatically">How to Create a Linked List Programmatically</h2>
<p>At this point, you should have basic knowledge of how a linked list works and its structure. Let’s create a linked list with the following steps:</p>
<ul>
<li><p>Create node.</p>
</li>
<li><p>Connect nodes.</p>
</li>
<li><p>Append nodes.</p>
</li>
<li><p>Insert nodes.</p>
</li>
<li><p>Delete nodes.</p>
</li>
</ul>
<h2 id="heading-how-to-create-a-node-in-a-linked-list">How to Create a Node in a Linked List</h2>
<p>As you know, a node consists of two parts: the data and the address to the next node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/10.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration of a single node</em></p>
<p>Here's how you can create a class called <code>Node</code>:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{
    <span class="hljs-keyword">int</span> data;
    Node next;

    Node(<span class="hljs-keyword">int</span> data) {
        <span class="hljs-keyword">this</span>.data = data;
        <span class="hljs-keyword">this</span>.next = <span class="hljs-keyword">null</span>;
    }
}
</code></pre>
<p>The <code>Node</code> class represents a node in a linked list, with two instance variables: data (holds the data stored in the node), and next (holds a reference to the next node in the list).</p>
<p>The constructor takes an <code>int</code> argument data to initialize the data variable and sets the next variable to null by default.</p>
<p>Now, you can simply create nodes and add data to them by creating new instances of the <code>Node</code> class:</p>
<pre><code class="lang-java"><span class="hljs-comment">// create nodes</span>
Node node1 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">11</span>);
Node node2 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">18</span>);
Node node3 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">24</span>);
</code></pre>
<p>In the code above, we created three nodes:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/11.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Showing the three nodes we created with the above code</em></p>
<h2 id="heading-how-to-link-the-nodes-in-a-linked-list">How to Link the Nodes in a Linked List</h2>
<p>After creating the nodes, you must connect them to form a linked list.</p>
<p>To do this you first need to create a linked list with a <code>head</code> node.</p>
<pre><code class="lang-bash">class LinkedList {

    Node head;

        <span class="hljs-function"><span class="hljs-title">LinkedList</span></span>() {
        this.head = null;
    }

}
</code></pre>
<p>Initially the <code>head</code> node is set to <code>null</code> because there are no nodes in the linked list yet.</p>
<p>Now to connect the nodes together in a Linked List, you can start by setting the <code>head</code> node to the first node in the list, in this case <code>node1</code>.</p>
<pre><code class="lang-bash">head = node1;
</code></pre>
<p>Then make the next of <code>node1</code> point to <code>node2</code>, and the next of <code>node2</code> point to <code>node3</code>. That is:</p>
<pre><code class="lang-java">node1.next = node2;
node2.next = node3;
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/19-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing the linking of nodes</em></p>
<p>You have successfully created a linked list and connected the nodes.</p>
<h2 id="heading-how-to-append-a-node-to-linked-list">How to Append a Node to Linked List</h2>
<p>Appending a node means adding a node to the end of a linked list. There are two cases to consider when appending a node:</p>
<ul>
<li><p>Appending to an empty linked list.</p>
</li>
<li><p>Appending to a non-empty linked list.</p>
</li>
</ul>
<h3 id="heading-how-to-append-a-node-to-an-empty-linked-list">How to Append a Node to an Empty Linked List</h3>
<p>If there are no nodes in a linked list, it is an empty linked list. To append a node to an empty linked list, you must first make sure the linked list is empty. You can do this by checking if the <code>head</code> node is <code>null</code>.</p>
<p>If the <code>head</code> node is <code>null</code> then you can simply set <code>head</code> to the new node:</p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (head == <span class="hljs-keyword">null</span>) {
    head = newNode;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/13.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Head node is null</em></p>
<h3 id="heading-how-to-append-a-node-to-non-empty-linked-list">How to Append a Node to Non-Empty Linked List</h3>
<p>If there are one or more nodes in a linked list, it is a non-empty linked list.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/19-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration of a non-empty linked list</em></p>
<p>To append a node to a non-empty linked list, make the last node link to the new node.</p>
<p>Unlike arrays, we cannot access any elements in a linked list directly. We must traverse from the <code>head</code> node to the <code>last</code> node.</p>
<p>To do that, create a temporary pointer (you can call the pointer <code>current</code>) that points to the head node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/17-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing to head node</em></p>
<p>Next, make <code>current</code> point to its <code>next</code> node, till the <code>next</code> of the current node points to <code>null</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/18-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing its next node</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/16-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing its next node</em></p>
<p>When the <code>next</code> node of <code>current</code> is <code>null</code>, you can then make the <code>next</code> of the <code>current</code> node point to the new node. That is:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/15-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing to new node</em></p>
<pre><code class="lang-java"><span class="hljs-keyword">while</span> (current.next != <span class="hljs-keyword">null</span>) {
    current = current.next;
}
current.next = newNode;
</code></pre>
<h2 id="heading-how-to-insert-a-node-in-a-linked-list">How to Insert a Node in a Linked List</h2>
<p>Inserting a node means adding a node to a given index. There are two cases to consider when inserting a node:</p>
<ul>
<li><p>Inserting a node at the first index.</p>
</li>
<li><p>Inserting a node at a given index.</p>
</li>
</ul>
<h3 id="heading-how-to-insert-a-node-at-the-first-index">How to Insert a node at the First Index</h3>
<p>To insert a node at the first index:</p>
<ul>
<li><p>make <code>next</code> of the new node point to the <code>head</code> node</p>
</li>
<li><p>set the <code>head</code> to the new node</p>
</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/13-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing new node pointing to head node</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/14-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing head node pointing to the new node</em></p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {
    newNode.next = head;
    head = newNode;
}
</code></pre>
<h3 id="heading-how-to-insert-a-node-at-any-position">How to Insert a Node at Any Position</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/4-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Linked List with index</em></p>
<p>Let’s suppose you want to add a node at index 2 in the linked list above.</p>
<p>To insert a node at index 2, you must traverse the node that comes before index 2.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/5-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing to head node</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/10-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing its next node</em></p>
<p>Next, create a new node and make the <code>next</code> of the new node point to the <code>next</code> of the <code>current</code> node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/9-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing new node pointing next node of current</em></p>
<p>Make the <code>next</code> of <code>current</code> point to the new node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/8-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing current pointing new node</em></p>
<p>Here's the code to do all this:</p>
<pre><code class="lang-java"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; index - <span class="hljs-number">1</span> &amp;&amp; current != <span class="hljs-keyword">null</span>; i++) {
    current = current.next;
}
<span class="hljs-keyword">if</span> (current != <span class="hljs-keyword">null</span>) {
    newNode.next = current.next;
    current.next = newNode;
}
</code></pre>
<h2 id="heading-how-to-delete-a-node-in-a-linked-list">How to Delete a Node in a Linked List</h2>
<p>There are two ways to delete nodes in a linked list:</p>
<ul>
<li><p>Deleting the head node.</p>
</li>
<li><p>Deleting a node at a given position.</p>
</li>
</ul>
<h3 id="heading-how-to-delete-the-head-node">How to Delete the Head Node</h3>
<p>Deleting the <code>head</code> node of a linked list is simple. You can store the data of the <code>head</code> node in a temporary variable if it needs to be accessed later. Then set the <code>head</code> pointer to point to the <code>next</code> node after the <code>head</code> node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/7-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing to head node</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing its next node</em></p>
<pre><code class="lang-java"><span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {
    deletedValue = head.data;
    head = head.next;
}
</code></pre>
<h3 id="heading-how-to-delete-a-node-at-a-given-position">How to Delete a Node at a Given Position</h3>
<p>Suppose you want to delete the node at index 2 in the diagram below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/4-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Linked List with index</em></p>
<p>You can delete the node at index 2 by making the node at index 1 point to the node at index 3.</p>
<p>To delete a node you must access the node you want to delete and the node before it. Take two temporary pointers (you can call the pointers <code>previous</code> and <code>current</code>). Let <code>previous</code> point to <code>null</code> and <code>current</code> point to the <code>head</code> node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/data--3-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (current) pointing to head node</em></p>
<p>Now, move <code>current</code> one step forward and move <code>previous</code> to <code>current</code> till you reach index 2.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/2-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer (previous) pointing to head node</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/3-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing temporary pointer pointing to its succeeding nodes</em></p>
<p>Make the <code>next</code> of <code>previous</code> point to the <code>next</code> of the <code>current</code> node.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/05/1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Illustration showing previous pointing to node next to current</em></p>
<p>Then store the data of <code>current</code> in a variable for future use.</p>
<p>After removing the link to the node at index 2, it is no longer accessible through any reference in the linked list.</p>
<p>It's important to note that when removing a node from a linked list, you don't need to explicitly delete the node itself at the given index. This is because the removed node will be automatically handled by the garbage collector when it is no longer reachable through any references.</p>
<p>However, in languages like C or C++, which do not have automatic garbage collection, you need to manually delete the node when it is no longer needed to avoid memory leaks and wasted memory resources.</p>
<pre><code class="lang-java"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; index &amp;&amp; current != <span class="hljs-keyword">null</span>; i++) {
    previous = current;
    current = current.next;
}

<span class="hljs-keyword">if</span> (current != <span class="hljs-keyword">null</span>) {
    deletedValue = current.data;
    previous.next = current.next;
}
</code></pre>
<h2 id="heading-complete-linked-list-code">Complete Linked List Code</h2>
<p>The code below shows a complete linked list. You can create, append, insert, delete, and display the nodes in the linked list:</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{

    <span class="hljs-keyword">int</span> data;
    Node next;

    Node(<span class="hljs-keyword">int</span> data) {
        <span class="hljs-keyword">this</span>.data = data;
        <span class="hljs-keyword">this</span>.next = <span class="hljs-keyword">null</span>;
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LinkedList</span> </span>{

    Node head;

    LinkedList() {
        <span class="hljs-keyword">this</span>.head = <span class="hljs-keyword">null</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">createLinkedList</span><span class="hljs-params">()</span> </span>{

        Node node1 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">11</span>);
        <span class="hljs-keyword">this</span>.head = node1;

        Node node2 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">18</span>);
        node1.next = node2;

        Node node3 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">24</span>);
        node2.next = node3;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">append</span><span class="hljs-params">(Node newNode)</span> </span>{

        Node current = <span class="hljs-keyword">this</span>.head;

        <span class="hljs-keyword">if</span> (current == <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">this</span>.head = newNode;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">while</span> (current.next != <span class="hljs-keyword">null</span>) {
                current = current.next;
            }
            current.next = newNode;
        }

    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insert</span><span class="hljs-params">(Node newNode, <span class="hljs-keyword">int</span> index)</span> </span>{

        Node current = <span class="hljs-keyword">this</span>.head;
        <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {
            newNode.next = current;
            <span class="hljs-keyword">this</span>.head = newNode;
        } <span class="hljs-keyword">else</span> {

            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; index - <span class="hljs-number">1</span> &amp;&amp; current != <span class="hljs-keyword">null</span>; i++) {
                current = current.next;
            }
            <span class="hljs-keyword">if</span> (current != <span class="hljs-keyword">null</span>) {
                newNode.next = current.next;
                current.next = newNode;
            }

        }

    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">delete</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index)</span> </span>{

        Node current = <span class="hljs-keyword">this</span>.head;
        Node previous = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">int</span> deletedValue = -<span class="hljs-number">1</span>;

        <span class="hljs-keyword">if</span> (index == <span class="hljs-number">0</span>) {
            deletedValue = <span class="hljs-keyword">this</span>.head.data;
            <span class="hljs-keyword">this</span>.head = <span class="hljs-keyword">this</span>.head.next;
            <span class="hljs-keyword">return</span> deletedValue;
        }

        <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; index &amp;&amp; current != <span class="hljs-keyword">null</span>; i++) {
                previous = current;
                current = current.next;

            }
            <span class="hljs-keyword">if</span> (current != <span class="hljs-keyword">null</span>) {

                deletedValue = current.data;
                previous.next = current.next;
            }
            <span class="hljs-keyword">return</span> deletedValue;
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">displayLinkedList</span><span class="hljs-params">()</span> </span>{

        Node current = <span class="hljs-keyword">this</span>.head;
        <span class="hljs-keyword">while</span> (current != <span class="hljs-keyword">null</span>) {
            System.out.println(current.data);
            current = current.next;

        }
    }

}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        LinkedList l1 = <span class="hljs-keyword">new</span> LinkedList();
        Node newNode1 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">22</span>);
        Node newNode2 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">43</span>);
        Node newNode3 = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">5</span>);
        l1.createLinkedList();

        l1.append(newNode1);
        l1.insert(newNode2, <span class="hljs-number">0</span>);
        l1.insert(newNode3, <span class="hljs-number">2</span>);
        l1.delete(<span class="hljs-number">2</span>);
        l1.displayLinkedList();
    }
}
</code></pre>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The linked list data structure can be used in various applications, such as web browsers and music players.</p>
<p>For example, in a web browser, the browser history can be stored as a linked list. Each page visited can be represented by a node, with the link pointing to the next page visited. This allows for easy navigation through the history, by simply traversing the linked list.</p>
<p>Similarly, in a music player, the playlist can be represented as a linked list. Each song can be represented by a node, with the link pointing to the next song in the playlist. This allows for easy navigation through the playlist, by simply traversing the linked list.</p>
<p>It is not very likely that you will create a linked list for your applications, as almost every programming language has a built-in linked list.</p>
<p>However, creating one and understanding its implementation can deepen your knowledge of the data structure. This knowledge can help you determine when to use a linked list over other data structures in real-life applications.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
