{"id":26914,"date":"2017-12-26T21:18:58","date_gmt":"2017-12-26T15:48:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26914"},"modified":"2018-10-27T18:12:32","modified_gmt":"2018-10-27T12:42:32","slug":"java-algorithm-split-circular-linked-list-two-halves","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-split-circular-linked-list-two-halves\/","title":{"rendered":"Split a Circular Linked List into two halves"},"content":{"rendered":"<h2 id=\"circular-linked-list\"><span style=\"color: #003366;\">Circular Linked List:<\/span><\/h2>\n<p>Circular Linked List is a list in which all nodes are connected to form <strong>circle<\/strong>. Here, the <strong>last node points to the first node<\/strong> in the list. Implemented by singly linked list or doubly linked list.<\/p>\n<h3 id=\"\"><\/h3>\n<h3 id=\"-2\" style=\"text-align: center;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26892\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1.png\" alt=\"Split a Circular Linked List into two halves\" width=\"722\" height=\"180\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1.png 722w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1-300x75.png 300w\" sizes=\"(max-width: 722px) 100vw, 722px\" \/><\/h3>\n<h3 id=\"original-linked-list\" style=\"text-align: center;\">Original Linked List<\/h3>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26893\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split1.png\" alt=\"Split a Circular Linked List into two halves\" width=\"429\" height=\"174\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split1.png 429w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split1-300x122.png 300w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><img decoding=\"async\" class=\"aligncenter wp-image-26892\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1.png\" alt=\"Split a Circular Linked List into two halves\" width=\"725\" height=\"181\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1.png 722w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_orig-1-300x75.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/p>\n<h3 id=\"result-linked-list-1\" style=\"text-align: center;\">Result Linked List 1<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-26894\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split2.png\" alt=\"Split a Circular Linked List into two halves\" width=\"432\" height=\"174\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split2.png 432w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/cll_split2-300x121.png 300w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/p>\n<h3 id=\"result-linked-list-2\" style=\"text-align: center;\">Result Linked List 2<\/h3>\n<h4 id=\"split-a-circular-linked-list-into-two-halves\"><span style=\"color: #800000;\"><strong>Split a Circular Linked List into two halves:\u00a0<\/strong><\/span><\/h4>\n<p><strong>Example:<\/strong> Explained through<a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-linked-list\" target=\"_blank\" rel=\"noopener\"> java program<\/a>.<\/p>\n<p><span style=\"color: #0000ff;\"><strong>Java programming:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to delete a node from doubly linked list<br\/> <br\/>class LinkedList {<br\/> <br\/>    static Node head, head1, head2;<br\/> <br\/>    static class Node {<br\/> <br\/>        int data;<br\/>        Node next, prev;<br\/> <br\/>        Node(int d) {<br\/>            data = d;<br\/>            next = prev = null;<br\/>        }<br\/>    }<br\/> <br\/>    \/* Function to split a list (starting with head) into two lists.<br\/>     head1_ref and head2_ref are references to head nodes of <br\/>     the two resultant linked lists *\/<br\/>    void splitList() {<br\/>        Node slow_ptr = head;<br\/>        Node fast_ptr = head;<br\/> <br\/>        if (head == null) {<br\/>            return;<br\/>        }<br\/> <br\/>        \/* If there are odd nodes in the circular list then<br\/>         fast_ptr-&gt;next becomes head and for even nodes <br\/>         fast_ptr-&gt;next-&gt;next becomes head *\/<br\/>        while (fast_ptr.next != head<br\/>                &amp;&amp; fast_ptr.next.next != head) {<br\/>            fast_ptr = fast_ptr.next.next;<br\/>            slow_ptr = slow_ptr.next;<br\/>        }<br\/> <br\/>        \/* If there are even elements in list then move fast_ptr *\/<br\/>        if (fast_ptr.next.next == head) {<br\/>            fast_ptr = fast_ptr.next;<br\/>        }<br\/> <br\/>        \/* Set the head pointer of first half *\/<br\/>        head1 = head;<br\/> <br\/>        \/* Set the head pointer of second half *\/<br\/>        if (head.next != head) {<br\/>            head2 = slow_ptr.next;<br\/>        }<br\/>        \/* Make second half circular *\/<br\/>        fast_ptr.next = slow_ptr.next;<br\/> <br\/>        \/* Make first half circular *\/<br\/>        slow_ptr.next = head;<br\/>    }<br\/> <br\/>    \/* Function to print nodes in a given singly linked list *\/<br\/>    void printList(Node node) {<br\/>        Node temp = node;<br\/>        if (node != null) {<br\/>            do {<br\/>                System.out.print(temp.data + &quot; &quot;);<br\/>                temp = temp.next;<br\/>            } while (temp != node);<br\/>        }<br\/>    }<br\/> <br\/>    public static void main(String[] args) {<br\/>        LinkedList list = new LinkedList();<br\/> <br\/>        \/\/Created linked list will be 12-&gt;56-&gt;2-&gt;11<br\/>        list.head = new Node(12);<br\/>        list.head.next = new Node(56);<br\/>        list.head.next.next = new Node(2);<br\/>        list.head.next.next.next = new Node(11);<br\/>        list.head.next.next.next.next = list.head;<br\/> <br\/>        System.out.println(&quot;Original Circular Linked list &quot;);<br\/>        list.printList(head);<br\/> <br\/>        \/\/ Split the list<br\/>        list.splitList();<br\/>        System.out.println(&quot;&quot;);<br\/>        System.out.println(&quot;First Circular List &quot;);<br\/>        list.printList(head1);<br\/>        System.out.println(&quot;&quot;);<br\/>        System.out.println(&quot;Second Circular List &quot;);<br\/>        list.printList(head2);<br\/>         <br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><span style=\"color: #003300;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Original Circular Linked List\r\n12 56 2 11\r\nFirst Circular Linked List\r\n12 56\r\nSecond Circular Linked List\r\n2 11<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Split a Circular Linked List into two halves &#8211; Circular Linked List is a list in which all nodes are connected to form circle.<\/p>\n","protected":false},"author":1,"featured_media":31596,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79477,79476],"tags":[80242,80246,80247,80243,80245,80244,80249,80248],"class_list":["post-26914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-circular-linked-list","category-linked-list","tag-check-if-linked-list-is-palindrome-java","tag-doubly-linked-list-palindrome","tag-doubly-linked-list-palindrome-java","tag-java-program-palindrome-with-linked-lists","tag-palindrome-checking-using-doubly-linked-list-in-c","tag-palindrome-linked-list-python","tag-palindrome-list-interviewbit","tag-singly-linked-list-palindrome-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26914"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26914\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31596"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}