Write a simple Linked List Node in python?

By Poornima, 7 months ago
  • Bookmark
0

how to create linked list in python?

Linked list
Python
1 Answer
0

A linked list is either:

  • the empty list, represented by None, or
  • a node that contains a cargo object and a reference to a linked list.
#! /usr/bin/env python
class Node:
    def __init__(self, cargo=None, next=None):
          self.car = cargo
          self.cdr = next
    def __str__(self):
          return str(self.car)
      
    def display(lst):
        if lst:
            w("%s " % lst)
            display(lst.cdr)
        else:
            w("nil\n")

Your Answer

Webinars

Live Masterclass on "Python for Artificial Intelligence"

Dec 4th (7:00 PM) 214 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More