Count rotations in sorted and rotated linked list in C++

C++Server Side ProgrammingProgramming

<p>We are given a linked list. The list is sorted first and then rotated by K number of nodes. The goal is to find the value of K. If we are given below linked list as input which is rotated by K number of nodes &minus;</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/46158/linked_list.jpg" class="fr-fic fr-dib" style="width: 527px; height: 144.047px;" width="527" height="144.047"></p><p>Then original must have been &minus;</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/46158/linked_list1.jpg" class="fr-fic fr-dib" style="width: 525px; height: 119.875px;" width="525" height="119.875"></p><p>And we can see K here is 2. Input linked list is a rotation of 2 nodes in the original sorted linked list.</p><p>Let us understand with examples.</p><p><strong>Input</strong> &minus; List : 5 &rarr; 7 &rarr; 9 &rarr; 1 &rarr; 3</p><p><strong>Output</strong>&nbsp;</p><p>Elements in the linked list are: 5 7 9 1 3</p><p>Count of rotations in sorted and rotated linked list are &minus; 3</p><p><strong>Explanation</strong> &minus; We can get the input list after three rotations in the original sorted list.</p><pre class="result notranslate">1 &rarr; 3 &rarr; 5 &rarr; 7 &rarr; 9, original 9 &rarr; 1 &rarr; 3 &rarr; 5 &rarr; 7, rotation 1 7 &rarr; 9 &rarr; 1 &rarr; 3 &rarr; 5, rotation 2 5 &rarr; 7 &rarr; 9 &rarr; 1 &rarr; 3 rotation 3</pre><p><strong>Input</strong> &minus; List &minus; 17 &rarr; 25 &rarr; 62 &rarr; 99</p><p><strong>Output</strong>&nbsp;</p><p>Elements in the linked list are &minus; 17 25 62 99</p><p>Count of rotations in sorted and rotated linked list are &minus; 4</p><p><strong>Explanation</strong>&minus; We can get the input list after four rotations in the original sorted list.</p><pre class="result notranslate">17 &rarr; 25 &rarr; 62 &rarr; 99, original 99 &rarr; 17 &rarr; 25 &rarr; 62, rotation 1 62 &rarr; 99 &rarr; 17 &rarr; 25, rotation 2 25 &rarr; 62 &rarr; 99 &rarr; 17, rotation 3 17 &rarr; 25 &rarr; 62 &rarr; 99, rotation 4</pre><h2>The approach used in the below program is as follows</h2><p>The input Linked list would have one point at which the next node is smaller than the previous one. If the input string is also sorted then it is full-rotation of the original one. Starting from the head node, traverse till the (current node&#39;s data &gt; head node&rsquo;s data) and increment count. In case where (current node&#39;s data &lt; head node&rsquo;s data) break the loop. The count will have a number of rotations in the original list to get an input list.</p><ul class="list"><li><p>Take an input list and insert elements in it.</p></li><li><p>Function insert_node(struct List_Node** head, int data) inserts nodes at the head of singly-linked lists with data.</p></li><li><p>Function print(struct List_Node* node) prints the input Linked list starting from head till the end using while loop.</p></li><li><p>Function rotations(struct List_Node* head) take the head pointer of the linked list and return the count of rotations done in the original linked list to get the input linked list.</p></li><li><p>Take the initial count as 0.</p></li><li><p>Take the temp variable as the data part of the head node.</p></li><li><p>Using while loop, traverse till the end of the linked list is reached. (head!=NULL).</p></li><li><p>Increment count in case every current node&rsquo;s data is greater than temp.</p></li><li><p>In case the current node&rsquo;s data is less than the head node&rsquo;s data ( temp). Break,</p></li><li><p>We will count the number of rotations.</p></li><li><p>Return count as result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/gAoke5uC" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;bits/stdc++.h&gt; using namespace std; struct List_Node{ &nbsp; &nbsp;int data; &nbsp; &nbsp;struct List_Node* next; }; int rotations(struct List_Node* head){ &nbsp; &nbsp;int count = 0; &nbsp; &nbsp;int temp = head-&gt;data; &nbsp; &nbsp;while (head != NULL){ &nbsp; &nbsp; &nbsp; if (temp &gt; head-&gt;data){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; count++; &nbsp; &nbsp; &nbsp; head = head-&gt;next; &nbsp; &nbsp;} &nbsp; &nbsp;return count; } void insert_node(struct List_Node** head, int data){ &nbsp; &nbsp;struct List_Node* new_node = new List_Node; &nbsp; &nbsp;new_node-&gt;data = data; &nbsp; &nbsp;new_node-&gt;next = (*head); &nbsp; &nbsp;(*head) = new_node; } void print(struct List_Node* node){ &nbsp; &nbsp;while (node != NULL){ &nbsp; &nbsp; &nbsp; cout&lt;&lt;node-&gt;data&lt;&lt;&quot; &quot;; &nbsp; &nbsp; &nbsp; node = node-&gt;next; &nbsp; &nbsp;} } int main(){ &nbsp; &nbsp;struct List_Node* head = NULL; &nbsp; &nbsp;insert_node(&amp;head, 2); &nbsp; &nbsp;insert_node(&amp;head, 1); &nbsp; &nbsp;insert_node(&amp;head, 18); &nbsp; &nbsp;insert_node(&amp;head, 35); &nbsp; &nbsp;insert_node(&amp;head, 28); &nbsp; &nbsp;cout&lt;&lt;&quot;Elements in the linked list are: &quot;; &nbsp; &nbsp;print(head); &nbsp; &nbsp;cout&lt;&lt;&quot; Count of rotations in sorted and rotated linked list are: &quot;&lt;&lt;rotations(head); &nbsp; &nbsp;return 0; }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Elements in the linked list are: 28 35 18 1 2 Count of rotations in sorted and rotated linked list are: 2</pre>
raja
Updated on 01-Dec-2020 12:52:56

Advertisements