Traversal

Traversals(Single pointer and two pointer)

Singly linked lists can be traversed in only forward direction starting form the first data element. We simply print the value of the next data element by assigning the pointer of the next node to the current data element.

class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

 
class SLinkedList:
    def __init__(self):
        self.headval = None

 
    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval

 
list = SLinkedList()
list.headval = Node("Mon")
e2 = Node("Tue")
e3 = Node("Wed")

 
# Link 1st Node to second node
list.headval.nextval = e2

 
# Link 2nd Node to third node
e2.nextval = e3

 
list.listprint()

When the above code is executed, the following result:

Mon
Tue
Wed 

Try here>>>