SlideShare a Scribd company logo
[Python] [Singly-linked list] [Element insertion] Consider the following singly linked list.
Provide the instructions to insert the new node immediately following the node containing 52.
Do not use a loop or any additional external references.
Solution
# 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
class LinkedList:
# constructor to initialize head
def __init__(self):
self.head = None
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def insert_After(self, prev_node, new_data):
# check if given prev_node exists
if prev_node is None:
print "previous node not in the LinkedList."
return
# create new node and put the data
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
#function to print the linked list
def print_the_List(self):
temp = self.head
while (temp):
print temp.data,
temp = temp.next
# Code execution starts
if __name__=='__main__':
# Start with the empty list
list = LinkedList()
# Insert 36 at the beginning
list.push(36);
# Insert 18 at the beginning. So linked list becomes 18->36->None
list.push(18);
# Insert 52 at the beginning. So linked list becomes 52->18->36->None
list.push(52);
list.push(2);
list.push(73);
print 'The list is:',
list.print_the_List()
# Insert 22, after 52. The linked list will becomes 73->2->22->52->18->36->None None
list.insert_After(list.head.next, 22)
print ' The list after inserting 22 is:',
list.print_the_List()

More Related Content

PDF
Please find the answer to the above problem as follows- Program.pdf
PPTX
Linked list part-2
PPTX
Linked List Presentation in data structurepptx
PDF
class LinkedList(object)    class _StackNode(object) .pdf
PPT
linked.ppt
PDF
Please refer this solution. This is working file for IntegersHeade.pdf
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PDF
Lab-2.2 717822E504.pdf
Please find the answer to the above problem as follows- Program.pdf
Linked list part-2
Linked List Presentation in data structurepptx
class LinkedList(object)    class _StackNode(object) .pdf
linked.ppt
Please refer this solution. This is working file for IntegersHeade.pdf
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
Lab-2.2 717822E504.pdf

Similar to [Python] [Singly-linked list] [Element insertion] Consider the follo.pdf (11)

PDF
Basic data structures in python
PDF
How to do insertion sort on a singly linked list with no header usin.pdf
PDF
TutorialII_Updated____niceupdateprogram.pdf
PPTX
single linked list
PPTX
Ll.pptx
PPTX
linked list.pptx
PDF
137 Lab-2.2.pdf
PPTX
DSModule2.pptx
PDF
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
PPT
Unit ii(dsc++)
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
Basic data structures in python
How to do insertion sort on a singly linked list with no header usin.pdf
TutorialII_Updated____niceupdateprogram.pdf
single linked list
Ll.pptx
linked list.pptx
137 Lab-2.2.pdf
DSModule2.pptx
I will provide my LinkedList from my last lab.LinkedList.cpp~~~~.pdf
Unit ii(dsc++)
Program to insert in a sorted list #includestdio.h#include.pdf
Ad

More from isenbergwarne4100 (20)

PDF
Identify and discuss the two basic principles of assessing risk tole.pdf
PDF
Help for this question please and have details For the combined ELSI.pdf
PDF
Explain the underlying reason that cell membranes have a bilayer str.pdf
PDF
Despite the repeated effort by the government to reform how Wall Str.pdf
PDF
Could you please fill this out for Postpartum depressionDirection.pdf
PDF
A university financial aid office polled an SRS of undergraduate stud.pdf
PDF
A confidential and voluntary survey conducted in STA 3024 in the Spr.pdf
PDF
1)Today, the overwhelming world leader in microprocessor sales i.pdf
PDF
1) Name the seven layers of the OSI network model, and 2) enumerate .pdf
PDF
8. Eenee, Meanie, and Miney each have a capital balance of $84,000 i.pdf
PDF
why Cu, Pb, and Sn were metals used in early civilizations Solu.pdf
PDF
Which scenario is the least likely to be supported by the theory of .pdf
PDF
What is the difference between neurotransmitters and neuropeptides .pdf
PDF
5) The US government is spying on Switzerland. They believe Switzerl.pdf
PDF
To qualify for the partial exclusion of gain on the sale or exchange.pdf
PDF
These questions are for Java.1. name of Java class with methods an.pdf
PDF
The United States Central Intelligence Agency, FBI, and National Sec.pdf
PDF
The study of consumer choose one brand over another and how they make.pdf
PDF
Survivin Monomer Plays an Essential Role in Apoptosis Regulation Mar.pdf
PDF
statistics Which is not true of p-values Select one When they are.pdf
Identify and discuss the two basic principles of assessing risk tole.pdf
Help for this question please and have details For the combined ELSI.pdf
Explain the underlying reason that cell membranes have a bilayer str.pdf
Despite the repeated effort by the government to reform how Wall Str.pdf
Could you please fill this out for Postpartum depressionDirection.pdf
A university financial aid office polled an SRS of undergraduate stud.pdf
A confidential and voluntary survey conducted in STA 3024 in the Spr.pdf
1)Today, the overwhelming world leader in microprocessor sales i.pdf
1) Name the seven layers of the OSI network model, and 2) enumerate .pdf
8. Eenee, Meanie, and Miney each have a capital balance of $84,000 i.pdf
why Cu, Pb, and Sn were metals used in early civilizations Solu.pdf
Which scenario is the least likely to be supported by the theory of .pdf
What is the difference between neurotransmitters and neuropeptides .pdf
5) The US government is spying on Switzerland. They believe Switzerl.pdf
To qualify for the partial exclusion of gain on the sale or exchange.pdf
These questions are for Java.1. name of Java class with methods an.pdf
The United States Central Intelligence Agency, FBI, and National Sec.pdf
The study of consumer choose one brand over another and how they make.pdf
Survivin Monomer Plays an Essential Role in Apoptosis Regulation Mar.pdf
statistics Which is not true of p-values Select one When they are.pdf
Ad

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
What if we spent less time fighting change, and more time building what’s rig...
Cell Structure & Organelles in detailed.
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Microbial diseases, their pathogenesis and prophylaxis
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
A systematic review of self-coping strategies used by university students to ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Updated Idioms and Phrasal Verbs in English subject
What if we spent less time fighting change, and more time building what’s rig...

[Python] [Singly-linked list] [Element insertion] Consider the follo.pdf

  • 1. [Python] [Singly-linked list] [Element insertion] Consider the following singly linked list. Provide the instructions to insert the new node immediately following the node containing 52. Do not use a loop or any additional external references. Solution # 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 class LinkedList: # constructor to initialize head def __init__(self): self.head = None # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node def insert_After(self, prev_node, new_data): # check if given prev_node exists if prev_node is None: print "previous node not in the LinkedList."
  • 2. return # create new node and put the data new_node = Node(new_data) new_node.next = prev_node.next prev_node.next = new_node #function to print the linked list def print_the_List(self): temp = self.head while (temp): print temp.data, temp = temp.next # Code execution starts if __name__=='__main__': # Start with the empty list list = LinkedList() # Insert 36 at the beginning list.push(36); # Insert 18 at the beginning. So linked list becomes 18->36->None list.push(18); # Insert 52 at the beginning. So linked list becomes 52->18->36->None list.push(52); list.push(2); list.push(73); print 'The list is:', list.print_the_List()
  • 3. # Insert 22, after 52. The linked list will becomes 73->2->22->52->18->36->None None list.insert_After(list.head.next, 22) print ' The list after inserting 22 is:', list.print_the_List()