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

    Join Date
    Feb 2013
    Rep Power

    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 :
    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)
    #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
    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.
    #otherwise, update the 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. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Rep Power
    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.

IMN logo majestic logo threadwatch logo seochat tools logo