Java program to find middle of a linked list
How to find middle of a linked list?
In here we are learning about a Java program to find the middle of a linked list. To find a middle of a linked , before that we should know that the number of link nodes are even or odd . If it’s odd then we have to take two pointer’s p1 and p2 as shown in the diagram p1 will move forward by one node and p2 will move forward two nodes. When p2 reaches the end of a linked list at that time p1 will be at the middle node of the linked list.
Insertion in the middle of linked list in Java
Java program to find the middle of a linked list, (even case) When given number of link nodes are even we will follow the same fashion ad we are doing in odd case but over here we will divide the number of link nodes by 2 so as to get the middle node of a linked list
Example:
input: 1–>2–>3–>4–>5
output
3
The middle of a given linked list is 3
Algorithm.
Step 1– Start
Step 2– Initialize a class
Step 3– Give input
Step 4– Node constructor
Step 5– Linked list constructor
Step 6– Add a function to find the size of a linked list. // return this.size
Step 7 – Check whether the linked list is empty or not // return this.size () == 0
Step 8– run a loop for traversing the pointer and then print the linked list
Step 9– Add a node in the beginning.
Step 10– Create a temp node which points towards the head
Step 11– Linked list is empty, then head and tail both is temp
Step 12– add a function for finding the mid node.
Step 13– Take two pointers p1 and p2
Step 14– Move p1 by one node and p2 by two nodes
Step 15– When p2 reaches the end of a linked list, p1 will point to the middle node.
Step 16– End.
Java Program to find middle of a Linked List
import java.lang.*; class LinkedList { Node head; // not using parameterized constructor would by default // force head instance to become null // Node head = null; // can also do this, but not required // Node Class class Node { int data; Node next; Node (int x) // parameterized constructor { data = x; next = null; } } public Node insert (int data) { // Creating newNode memory & assigning data value Node newNode = new Node (data); // assigning this newNode's next as current head node newNode.next = head; // re-assigning head to this newNode head = newNode; return head; } public void display () { Node node = head; //as linked list will end when Node reaches Null while (node != null) { System.out.print (node.data + " "); node = node.next; } System.out.println (); } public int mid () { return this.midNode ().data; } //Function to find the mid node private Node midNode () { //Take two pointers p1 and p2 Node p1 = this.head; Node p2 = this.head; //Move p1 by one node and p2 by two nodes while (p2.next != null && p2.next.next != null) { p1 = p1.next; p2 = p2.next.next; } //When p2 reaches the end, p1 will point to the mid node return p1; } } class Main { public static void main (String args[]) { LinkedList ll = new LinkedList (); ll.insert (6); ll.insert (5); ll.insert (3); ll.insert (4); ll.insert (2); ll.display (); System.out.println ("Middle of the linked list is :- "); System.out.println (ll.mid ()); } }
Output: 2 4 3 5 6 Middle of the linked list is :- 3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
public node findmid(){
int mid=(size()%2)+(size()/2);
node temp=head;
while(mid–>1){
temp=temp.next;
d=temp.data;
}
return temp;
}