Find middle element of circular linked list in pythonPython program for Find middle element of circular linked list. Here problem description and explanation. Show
Output 1 2 3 4 5 6 7 Middle Node is : 4Find middle element of circular linked list in javaJava program for Find middle element of circular linked list. Here more information. // Java Program // Find middle element in circular linked list class LinkNode { public int data; public LinkNode next; public LinkNode(int data) { this.data = data; this.next = null; } } class CircularLinkedList { public LinkNode head; // Class constructor public CircularLinkedList() { this.head = null; } // Add element at end of circular linked list public void insert(int data) { // Create a new node LinkNode node = new LinkNode(data); if (this.head == null) { this.head = node; } else { LinkNode temp = this.head; // Find the last node while (temp.next != this.head) { // Visit to next node temp = temp.next; } // Add node temp.next = node; } // Connect last node to head node.next = this.head; } // Display node element of circular linked list public void display() { if (this.head == null) { System.out.println("Empty Linked List"); } else { LinkNode temp = this.head; // iterate linked list while (temp != null) { // Display node System.out.print(" " + temp.data); // Visit to next node temp = temp.next; if (temp == this.head) { // Stop iteration return; } } } } // Find middle node of given circular linked list public void findMiddle() { if (this.head == null) { System.out.println("Empty Linked List"); } else { if (this.head.next != this.head && this.head.next.next != this.head) { LinkNode temp = this.head; LinkNode middle = this.head; //Find middle node while (temp != null && temp.next != this.head && temp.next.next != this.head) { middle = middle.next; temp = temp.next.next; } System.out.println("\n Middle Node is : " + middle.data); } else { System.out.println("\nLess than three nodes"); } } } public static void main(String[] args) { CircularLinkedList cll = new CircularLinkedList(); // Add linked list nodes cll.insert(1); cll.insert(2); cll.insert(3); cll.insert(4); cll.insert(5); cll.insert(6); cll.insert(7); cll.display(); cll.findMiddle(); } }Output 1 2 3 4 5 6 7 Middle Node is : 4Q. Program to insert a new node at the middle of the circular linked list.ExplanationIn this program, we create a circular linked list and insert a new node in the middle of the list. If the list is empty, both head and tail will point to new node. If the list is not empty, then we will calculate the size of the list and divide it by 2 to get the mid-point of the list where the new node needs to be inserted. After inserting the new node in the middle of the list. Consider the above diagram; the new node needs to be added to the middle of the list. First, we calculate the size which in this case is 4. So, to get the mid-point, we divide it by 2 and store it in a variable count. We will define two nodes current, and temp such that temp will point to head, and current will point to the node previous to temp. We iterate through the list till mid-point is reached by incrementing temp to temp.next then, insert the new node in between current and temp. Current'next node will be new and the new's next node will be temp. Algorithm
Q. Program to delete a new node from the middle of the circular linked list.ExplanationIn this program, we will create a circular linked list and delete a node from the middle of the list. If the list is empty, display the message "List is empty". If the list is not empty, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list. We maintain two pointers temp and current. Current will point to the previous node of temp. We will iterate through the list until mid-point is reached then the current will point to the middle node. We delete middle node such that current's next will be temp's next node. Circular linked list after deleting node from middle of the list Consider the above list. Size of the list is 4. Mid-point of the node is 2. To remove C from the list, we will iterate through the list till mid-point. Now current will point to B and temp will point to C. C will be removed when B will point to D. Algorithm
Circular Linked ListCircular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required. Application of Circular Linked List
Implementing Circular Linked ListImplementing a circular linked list is very easy and almost similar to linear linked list implementation, with the only difference being that, in circular linked list the last Node will have it's next point to the Head of the List. In Linear linked list the last Node simply holds NULL in it's next pointer. So this will be oue Node class, as we have already studied in the lesson, it will be used to form the List. class Node { public: int data; //pointer to the next node node* next; node() { data = 0; next = NULL; } node(int x) { data = x; next = NULL; } }Circular Linked ListCircular Linked List class will be almost same as the Linked List class that we studied in the previous lesson, with a few difference in the implementation of class methods. Insertion at the BeginningSteps to insert a Node at beginning :
Insertion at the EndSteps to insert a Node at the end :
Searching for an Element in the ListIn searhing we do not have to do much, we just need to traverse like we did while getting the last node, in this case we will also compare the data of the Node. If we get the Node with the same data, we will return it, otherwise we will make our pointer point the next Node, and so on. node* CircularLinkedList :: search(int x) { node *ptr = head; while(ptr != NULL && ptr->data != x) { //until we reach the end or we find a Node with data x, we keep moving ptr = ptr->next; } return ptr; }Deleting a Node from the ListDeleting a node can be done in many ways, like we first search the Node with data which we want to delete and then we delete it. In our approach, we will define a method which will take the data to be deleted as argument, will use the search method to locate it and will then remove the Node from the List. To remove any Node from the list, we need to do the following :
C++ Code/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int n = 0;
ListNode* temp = head;
while(temp) {
n++;
temp = temp->next;
}
temp = head;
for(int i = 0; i < n / 2; i++) {
temp = temp->next;
}
return temp;
}
}; Solution 2: [Efficient] Tortoise-Hare-Approach Unlike the above approach, we don’t have to maintain nodes count here and we will be able to find the middle node in a single traversal so this approach is more efficient. Intuition: In the Tortoise-Hare approach, we increment slow ptr by 1 and fast ptr by 2, so if take a close look fast ptr will travel double than that of the slow pointer. So when the fast ptr will be at the end of Linked List, slow ptr would have covered half of Linked List till then. So slow ptr will be pointing towards the middle of Linked List. Approach:
Code: C++ Code/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next)
slow = slow->next, fast = fast->next->next;
return slow;
}
};
|