code to generate linked list or tree from list