title: Explanation about pointers vs manipulating objects in a linked list
body: <p>I am having trouble wrapping my head around the concept of switching pointers vs actual manipulation of objects in a linked list.</p>
  
  <p>Here is the code for building the linked list from scratch</p>
  
  <pre><code>class ListNode:
  
      def __init__(self, val:int, nextNode=None):
          self.val = val
          self.next = nextNode
  
  class LinkedList:
  
      def __init__(self, val=None):
  
          self.head = None
          self.tail = None
  
      def addValue(self, val:int):
          if self.head == None:
              self.head = self.tail = ListNode(val)
  
          else:
              self.tail.next = ListNode(val)
              self.tail = self.tail.next
          return self.tail
  
      def addMultiple(self, values:list):
  
          for i in values:
              self.addValue(i)
  
  
      def add_to_beginning(self, val):
          if self.head == None:
              self.head = self.tail = ListNode(val)
          else:
              self.head = ListNode(val, self.head)
  
      def display(self):
  
          elems = []
  
          curr = self.head
          while curr:
              elems.append(curr.val)
              curr = curr.next
  
          print(elems)
  
  </code></pre>
  
  <p>creating a linked list here:</p>
  
  <pre><code>l1 = LinkedList()
  l1.addMultiple([1,2,3,4,5])
  </code></pre>
  
  <p>For example if I wanted to move the nth element to the head, so I created this function</p>
  
  <pre><code>class Solution:
  
      def move_n_to_head(self, head, n):
  
          if head == None:
              return None
          if head.next == None:
              return head
  
          temp = None
          count = 0
          dummy = fast = slow = ListNode(0)
  
          fast.next = head
          while fast:
  
              if count == n:
                  temp = fast.next
                  fast.next = fast.next.next #why does this line manipuate the head?
                  break
  
              fast = fast.next #why does this line NOT manipulate the head?
              count += 1
  
          slow.next = temp
          slow.next.next = head
          return dummy.next
  </code></pre>
  
  <p>Everything works fine and I got the solution I wanted, but specifically I don't understand why this line <strong>does</strong> manipulate the head?</p>
  
  <pre><code>fast.next = fast.next.next
  </code></pre>
  
  <p>After using this above line in the 3rd iteration, the head now becomes [1,2,3,5] </p>
  
  <p>However while this line <strong>does not</strong> manipulates the head as I am traversing through the list? The head remains [1,2,3,4,5] after every iteration?</p>
  
  <pre><code>fast = fast.next
  </code></pre>
  
  <p>I read this other stackoverflow explanation on dummy node pointers which was helpful but I still don't understand it. </p>
  
  <p><a href="https://stackoverflow.com/questions/58715870/explanation-about-dummy-nodes-and-pointers-in-linked-lists">Explanation about dummy nodes and pointers in linked lists</a></p>
  
  <p>Thanks in advance!</p>