### Thread: Linked list within a Linked List

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
1
Rep Power
0

#### Linked list within a Linked List

im completely stuck on how to make a linked list within a linked list.

i know how to create a linked list:

#A linked list class
class SingleSortedLinkedList :
#constructor
def __init__ (self) :
self._size = 0
self._head = None

#check for empty list
def isEmpty(self) :
return self._size == 0

#overloaded length
def __len__(self) :
return self._size

#prints out the data
def traversal(self) :
current = self._head
while current is not None :
print (current._data)
current = current._next

#insets (sorted) a new data item into the list
def insert(self, item) :
newNode = ListNode(item)
self._size=self._size+1
#when our list is new (or reduced to no items)
if self._head is None :
self._head = newNode
#If our new node goes first:
elif item < self._head._data :
newNode._next = self._head
self._head = newNode
#not at the head, so we will be inserting later into the list
else:
node = self._head
#loop to advance pointer until insertion spot located
while node is not None and node._data < item :
prev = node
node = node._next
if node is None:
#at end, so newNode already points to None like it should.
prev._next=newNode
else:
#otherwise, update the node
prev._next=newNode
newNode._next=node

#class for a single node
class ListNode :
def __init__ ( self, data) :
self._data = data
self._next = None

just don't know how to modify it so each node point to a separate linked list
2. Can you be more specific what you mean with "each node point to a separate linked list"?

Your node have a data node and a next node.
If you want the node to point to two different node, you will have to create an extra node, e.g. next2.

Maybe the binary tree can give you an idea to what you want.
Might not be what you are looking for, but in this example, you can see how one node can point at two "different" lists by using a left and right as "next" nodes.