SlideShare a Scribd company logo
1 of 3
Download to read offline
[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

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

Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
When writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfWhen writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfaadyaenterprisesnoid
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavRohit malav
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxssusera965f6
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfajaycosmeticslg
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 

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

Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
When writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdfWhen writing a member function of the HAMT class- the member function.pdf
When writing a member function of the HAMT class- the member function.pdf
 
LINKED LISTS
LINKED LISTSLINKED LISTS
LINKED LISTS
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
 
Linked list
Linked listLinked list
Linked list
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Please write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdfPlease write in C++ and should be able to compile and debug.Thank yo.pdf
Please write in C++ and should be able to compile and debug.Thank yo.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Linked list
Linked listLinked list
Linked list
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Linked list
Linked listLinked list
Linked list
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 

More from isenbergwarne4100

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

More from isenbergwarne4100 (20)

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

Recently uploaded

ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 

Recently uploaded (20)

VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 

[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()