insert a node at the middle of the singly linked list- DATA STRUCTURESWrite a program to insert a node at the middle of the singly linked list.Note :The linked list should be 6 7 8 9 before the insertion of a new node.Constraints:.Example:Input FormatInput to get an integer(node).Output FormatDisplay the output as shown in the sample output.Explanation:.Public Test Cases:# INPUT EXPECTED OUTPUT1 56 7 8 9 6 7 5 8 92 616 7 8 9 6 7 61 8 9
Question
insert a node at the middle of the singly linked list- DATA STRUCTURESWrite a program to insert a node at the middle of the singly linked list.Note :The linked list should be 6 7 8 9 before the insertion of a new node.Constraints:.Example:Input FormatInput to get an integer(node).Output FormatDisplay the output as shown in the sample output.Explanation:.Public Test Cases:# INPUT EXPECTED OUTPUT1 56 7 8 9 6 7 5 8 92 616 7 8 9 6 7 61 8 9
Solution 1
Here is a Python program that inserts a node at the middle of a singly linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
else:
cur = self.head
while cur.next:
cur = cur.next
cur.next = Node(data)
def display(self):
elems = []
cur_node = self.head
while cur_node:
elems.append(str(cur_node.data))
cur_node = cur_node.next
print(' '.join(elems))
def insert_middle(self, data):
if self.head is None:
self.head = Node(data)
else:
fast = self.head
slow = self.head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
new_node = Node(data)
new_node.next = slow.next
slow.next = new_node
# Initialize linked list and append initial elements
ll = LinkedList()
ll.append(6)
ll.append(7)
ll.append(8)
ll.append(9)
# Insert new node in the middle
ll.insert_middle(5)
# Display the linked list
ll.display()
This program first creates a linked list with the elements 6, 7, 8, 9. Then it inserts a new node with the value 5 at the middle of the list. Finally, it displays the linked list. The output will be: 6 7 5 8 9.
You can replace the value 5 in the ll.insert_middle(5) line with any other integer to insert a different node.
Solution 2
Here is a Python program that inserts a node at the middle of a singly linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = Node()
def append(self, data):
new_node = Node(data)
cur = self.head
while cur
Solution 3
Here is a Python program that inserts a node at the middle of a singly linked list:
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
else:
cur = self.head
while cur.next:
cur = cur.next
cur.next = Node(data)
def display(self):
elems = []
cur_node = self.head
while cur_node:
elems.append(str(cur_node.data))
cur_node = cur_node.next
print(' '.join(elems))
def insert_middle(self, data):
slow_ptr = self.head
fast_ptr = self.head
prev = None
while(fast_ptr is not None and fast_ptr.next is not None):
fast_ptr = fast_ptr.next.next
prev = slow_ptr
slow_ptr = slow_ptr.next
new_node = Node(data)
prev.next = new_node
new_node.next = slow_ptr
# Initialize linked list and append data
ll = LinkedList()
ll.append(6)
ll.append(7)
ll.append(8)
ll.append(9)
# Insert data in the middle
ll.insert_middle(5)
# Display the linked list
ll.display()
This program first creates a linked list with the elements 6, 7, 8, 9. Then it inserts the number 5 at the middle of the list. Finally, it prints the updated list. The output will be: 6 7 5 8 9.
You can replace the number 5 in the ll.insert_middle(5) line with any number you want to insert into the middle of the list.
Similar Questions
Complete the function create_node(). Function will receive an integer as input, create a singly linked list type node for it, store the input inside the node and return the address of the node.Note: Structure for singly Linked List is predefined, do refer it.Input Format:No need to read any input as it is predefined, just complete the function.Output Format:No need to print any output.Constraints:1<= input <= 10000Sample Input 1:23 98 76 45 12 34 89 100 34 12 37 -1Sample Output 1:23 98 76 45 12 34 89 100 34 12 37Sample Input 2:10 20 30 40 50 60 -1Sample Output 2:10 20 30 40 50 60
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its attribute, insert this node at the desired position and return the head node.A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.Example refers to the first node in the list Insert a node at position with . The new list is Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.insertNodeAtPosition has the following parameters:head: a SinglyLinkedListNode pointer to the head of the listdata: an integer value to insert as data in your new nodeposition: an integer position to insert the new node, zero based indexingReturnsSinglyLinkedListNode pointer: a reference to the head of the revised listInput FormatThe first line contains an integer , the number of elements in the linked list.Each of the next lines contains an integer SinglyLinkedListNode[i].data.The next line contains an integer , the data of the node that is to be inserted.The last line contains an integer .Constraints, where is the element of the linked list..Sample Input31613712Sample Output16 13 1 7ExplanationThe initial linked list is . Insert at the position which currently has in it. The updated linked list is .
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.Function DescriptionComplete the insertNodeAtTail function in the editor below.insertNodeAtTail has the following parameters:SinglyLinkedListNode pointer head: a reference to the head of a listint data: the data value for the node to insertReturnsSinglyLinkedListNode pointer: reference to the head of the modified linked listInput FormatThe first line contains an integer , the number of elements in the linked list.The next lines contain an integer each, the value that needs to be inserted at tail.ConstraintsSample InputSTDIN Function ----- -------- 5 size of linked list n = 5 141 linked list data values 141..474 302 164 530 474Sample Output141302164530474ExplanationFirst the linked list is NULL. After inserting 141, the list is 141 -> NULL.After inserting 302, the list is 141 -> 302 -> NULL.After inserting 164, the list is 141 -> 302 -> 164 -> NULL.After inserting 530, the list is 141 -> 302 -> 164 -> 530 -> NULL. After inserting 474, the list is 141 -> 302 -> 164 -> 530 -> 474 -> NULL, which is the final list.
Write program to perform INSERT_FIRST (to insert a node at the first position) andREVERSE_TRAVERSE (to display the data in nodes in reverse order) operations indoubly linked list
nsert a Node at the Beginning of a Doubly Linked ListInsert a Node at the Beginning of a Doubly Linked ListConstraints:NAExample:Sample Input:33215Sample Output:5 1 2 3 Explanation:Input: 3 (Size), 3 2 1 (Initial elements), 5 (Insert)Output: List after insertion: 5 1 2 3
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.