title: Concatenate Python Linked List
body: <p>I am attempting to concatenate a Python linked list without copying the data contained within the nodes of the list. I have a function that will concatenate the list using copies of the nodes passed in, but I can't seem to get the function that doesn't use copies to work. </p>
  
  <p>These functions are for testing and timing purposes; I know that Python's built-in list is awesome!</p>
  
  <p>Here is the class I have been working with and the concatenate function.</p>
  
  <pre><code>class Cell:
      def __init__( self, data, next = None ):
          self.data = data
          self.next = next
  
  def print_list(self):
      node = self
      while node != None:
          print node.data
          node = node.next
  </code></pre>
  
  <p>The concatenation function is not meant to be a member function of the Cell class.</p>
  
  <pre><code>def list_concat(A, B):
      while A.next != None:
          A = A.next
      A.next = B      
      return A
  </code></pre>
  
  <p>This function overwrites the first element of a list if the parameter A has more than one node. I understand why that is happening, but am not sure how to go about fixing it.</p>
  
  <p>Here is the testing code I've been using for this function.</p>
  
  <pre><code>e = Cell(5)
  test = Cell(3, Cell(4))
  test2 = list_concat(test2, e)   
  test2.print_list()
  </code></pre>
  
  <p>Any insight or help would be greatly appreciated.</p>
  
  <p>*edited to fix code formatting</p>