Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python linked list insert

# A simple Python program to introduce a linked list
 
# Node class
class Node:
 
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data  # Assign data
        self.next = None  # Initialize next as null
 
 
# Linked List class contains a Node object
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
 
# Code execution starts here
if __name__=='__main__':
 
    # Start with the empty list
    llist = LinkedList()
 
    llist.head = Node(1)
    second = Node(2)
    third = Node(3)
 
    '''
    Three nodes have been created.
    We have references to these three blocks as head,
    second and third
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  | None |     | 2  | None |     |  3 | None |
    +----+------+     +----+------+     +----+------+
    '''
 
    llist.head.next = second; # Link first node with second
 
    '''
    Now next of first Node refers to second.  So they
    both are linked.
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  |  o-------->| 2  | null |     |  3 | null |
    +----+------+     +----+------+     +----+------+
    '''
 
    second.next = third; # Link second node with the third node
 
    '''
    Now next of second Node refers to third.  So all three
    nodes are linked.
 
    llist.head        second              third
         |                |                  |
         |                |                  |
    +----+------+     +----+------+     +----+------+
    | 1  |  o-------->| 2  |  o-------->|  3 | null |
    +----+------+     +----+------+     +----+------+
    '''
Comment

PREVIOUS NEXT
Code Example
Python :: python list to sublists 
Python :: from django.urls import path 
Python :: python subprocess no such file or directory 
Python :: python infinite loop 
Python :: string to list of characters python 
Python :: time complexity of remove in python 
Python :: renamecolumns pandas 
Python :: make guessing game by python 
Python :: My flask static first file 
Python :: print backwards python 
Python :: pyglet on button press 
Python :: python seq 
Python :: import turtle t=turtle.turtle() def star(t): for t in range(5): t.color("red") t.pendown() t.begin_fill() t.forward(100) t.right(144) t.end_fill() 
Python :: EJERCICIOS DE FOR Y WHILE en python 
Python :: turtle opacity 
Python :: bassie en adriaan 
Python :: python random password generator 
Python :: ftplib tqdm 
Python :: gcp functions save BQ 
Shell :: lumen run command 
Shell :: stop apache server 
Shell :: docker install nano 
Shell :: remove proxy git 
Shell :: mvn clean install skip tests 
Shell :: install netstat ubuntu 
Shell :: search for port localhost mac 
Shell :: como instalar telegram ubuntu 
Shell :: uninstall imagemagick ubuntu 
Shell :: uninstall cocoapods 
Shell :: install ninja ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =