Sunday, May 18, 2008

Algorithm of copying a Linked List


Algorithm of copying a Linked List:

  • Check if the list is non-empty and then only proceed otherwise return
  • Iterate through the source list
  • Create a new node for the destination list
  • Set the element of this node to that of the current node of the source list
  • If it's the first node then assign the tail of the new list to this node
  • Else set the tail to the link of current tail
  • Set the link of the tail to 'null'
  • Continue with the rest of the elements of the source list
  • Return the head of the new list



Share/Save/Bookmark


No comments: