title: How a LinkedList Keeps Track of All Nodes - C#?
body: <p>I have been struggling trying to develop my own singly linked list, I cant understand how a node is inserted at the end of a Linked List? </p>
  
  <p>Here's the code: </p>
  
  <pre><code>class LinkedList
  {
      private Node head;
  
      public void AddLast(int value)
      {
          if (head == null)
          {
              head = new Node();
  
              head.value = value;
              head.next = null;
          }
          else
          {
              Node temp = new Node();
              temp.value = value;
  
              Node current = head;
  
              while (current.next != null)
              {
                  current = current.next;
              }
  
              current.next = temp;
          }
      }
  
      public void PrintAll() 
      {
          Node current = head;
  
          while (current != null)
          {
              Console.WriteLine(current.value);
              current = current.next;
          }
  
      }
  }
  </code></pre>
  
  <p>Here is the main method</p>
  
  <pre><code>    static void Main(string[] args)
      {
          LinkedList list = new LinkedList();
          list.AddLast(3);
          list.AddLast(5);
          list.AddLast(4);
      }
  </code></pre>
  
  <p>1) I totally get the first part list.AddLast(3). Since head is null, we create a new node head and assign values to it.</p>
  
  <p>2) When list.AddLast(5) is called, head is no more null, and thus we create a new temporary node, assign values to it. NOW we create a new node current which holds values of Head, it is to be noted Head.Next was null. </p>
  
  <p>Now we iterate through current and place our temp node to current.Next. </p>
  
  <p>3) Now, Upon calling list.AddLast(5) shouldn't the current be again overwritten with the contents of Head? which was Head.Value = 3 and Head.Next = Null.</p>
  
  <p>So shouldn't it current.Value = 3 and Current.Next = Null? And if not then why? </p>