JAVA Program to Split Circular Linked List into two halves

how to prepare for tcs itp interview

JAVA Program to Split a Circular Linked List into two equal halves

To split a circular linked list into two halves all we have to do is make two heads point to two different elements. The first head will be pointing towards the first element like before but the second head will point to the element that will become the first element of the second circular linked list. The references stored in the last nodes will also change for both of the lists. Let’s take a look at the Algorithm and Program for the same.

Steps to be followed to Split a Circular Linked List in to two halves

  • Firstly the number of elements in the circular linked list should be known to us.
  • Divide the linked list using (i+1)/2 value .
  • The first circular linked list will be from first node to (i+1)/2 – 1 position.
  • The second circular linked list will be from (i+1)/2 to the last node.
  • Change the reference of the node of (i+1)/2 – 1 th position to the first element and the first half is done.
  • Make a new header that points at the (i+1)/2 th node, making it the first node of the second circular list. Also, change the reference the last node is pointing towards to the (i+1)/2th node making it the first element of the second circular linked list.
  • In this way, you can split a circular linked list into two halves.
Split a Circular Linked List in two halves

Algorithm

  • split()
  • Node l1 = HEAD
  • Node l2 = HEAD
  • FOR i=0 until  l2.next != head && l2.next.next != head , i++
    • l2 = l2->next->next
    • l1 = l1->next
  • IF l2->next->next == HEAD
    • l2 = l2->next
  • list1 = HEAD
  • IF HEAD->next != HEAD
    • list2 = l1->next
  • l2->next = l1->next;
  •  l1->next = HEAD

JAVA Program to Split A Circular Linked List

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • Circular Linked List in –
    C | C++ | Java
  • Insertion in Circular Linked List –
    C | C++ | Java
  • Insertion at the beginning–
    C | C++ | Java
  • Insertion at the end –
    C | C++ | Java
  • Insertion at nth position –
    C | C++ | Java
  • Deletion in Circular Linked List –
    C | C++ | Java
  • Deletion from beginning in Circular Linked List –
    C | C++ | Java
  • Deletion from nth position in Circular Linked List –
  • Deletion from end in Circular Linked List –
    C | C++ | Java
  • Insertion and Deletion in Circular Linked List – C | C++ | Java
  • Split a Circular Linked List in two halves –
    C | C++ | Java
  • Count nodes in Circular Linked List –
    C | C++ | Java
  • Sorted Insert In Circular Linked List –
    C | C++ | Java
  • Insertion in the middle in Circular Linked List –
    C | C++ | Java

Circular Linked List