{"id":27667,"date":"2018-04-09T20:16:05","date_gmt":"2018-04-09T14:46:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27667"},"modified":"2018-09-16T13:26:21","modified_gmt":"2018-09-16T07:56:21","slug":"c-algorithm-move-last-element-front-given-linked-list-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-move-last-element-front-given-linked-list-2\/","title":{"rendered":"C Algorithm &#8211; Move last element to front of a given Linked List"},"content":{"rendered":"<p>Write a C function that moves last element to front in a given Singly Linked List. For example, if the given Linked List is 1-&gt;2-&gt;3-&gt;4-&gt;5, then the function should change the list to 5-&gt;1-&gt;2-&gt;3-&gt;4.<span id=\"more-6850\"><\/span><\/p>\n<p>Algorithm:<br \/>\nTraverse the list till last node. Use two pointers: one to store the address of last node and other for address of second last node. After the end of loop do following operations.<br \/>\ni) Make second last as last (secLast-&gt;next = NULL).<br \/>\nii) Set next of last as head (last-&gt;next = *head_ref).<br \/>\niii) Make last as head ( *head_ref = last)<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* C Program to move last element to front in a given linked list *\/<br\/>#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* A linked list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node *next;<br\/>};<br\/> <br\/>\/* We are using a double pointer head_ref here because we change<br\/>   head of the linked list inside this function.*\/<br\/>void moveToFront(struct node **head_ref)<br\/>{<br\/>    \/* If linked list is empty, or it contains only one node,<br\/>      then nothing needs to be done, simply return *\/<br\/>    if (*head_ref == NULL || (*head_ref)-&gt;next == NULL)<br\/>        return;<br\/> <br\/>    \/* Initialize second last and last pointers *\/<br\/>    struct node *secLast = NULL;<br\/>    struct node *last = *head_ref;<br\/> <br\/>    \/*After this loop secLast contains address of second last<br\/>    node and last contains address of last node in Linked List *\/<br\/>    while (last-&gt;next != NULL)<br\/>    {<br\/>        secLast = last;<br\/>        last = last-&gt;next;<br\/>    }<br\/> <br\/>    \/* Set the next of second last as NULL *\/<br\/>    secLast-&gt;next = NULL;<br\/> <br\/>    \/* Set next of last as head node *\/<br\/>    last-&gt;next = *head_ref;<br\/> <br\/>    \/* Change the head pointer to point to last node now *\/<br\/>    *head_ref = last;<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Function to add a node at the begining of Linked List *\/<br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>        (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/> <br\/>\/* Function to print nodes in a given linked list *\/<br\/>void printList(struct node *node)<br\/>{<br\/>    while(node != NULL)<br\/>    {<br\/>        printf(&quot;%d &quot;, node-&gt;data);<br\/>        node = node-&gt;next;<br\/>    }<br\/>}<br\/> <br\/>\/* Druver program to test above function *\/<br\/>int main()<br\/>{<br\/>    struct node *start = NULL;<br\/> <br\/>    \/* The constructed linked list is:<br\/>     1-&gt;2-&gt;3-&gt;4-&gt;5 *\/<br\/>    push(&amp;start, 5);<br\/>    push(&amp;start, 4);<br\/>    push(&amp;start, 3);<br\/>    push(&amp;start, 2);<br\/>    push(&amp;start, 1);<br\/> <br\/>    printf(&quot;\\n Linked list before moving last to front\\n&quot;);<br\/>    printList(start);<br\/> <br\/>    moveToFront(&amp;start);<br\/> <br\/>    printf(&quot;\\n Linked list after removing last to front\\n&quot;);<br\/>    printList(start);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre> Linked list before moving last to front \r\n1 2 3 4 5 \r\n Linked list after removing last to front \r\n5 1 2 3<\/pre>\n<p>Time Complexity: O(n) where n is the number of nodes in the given Linked List.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Move last element to front of a given Linked List &#8211; Linked List &#8211; Write a C function that moves last element to front in a given Singly<\/p>\n","protected":false},"author":1,"featured_media":31285,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81917,81924,81919,81920,81922,81925,81918,81586,81923,81921],"class_list":["post-27667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-java-linked-list-move-element-to-front","tag-java-list-move-element-to-another-position","tag-move-last-element-to-front-of-a-given-linked-list-java","tag-move-node-from-one-linked-list-to-another","tag-move-to-front-java","tag-sorted-vector","tag-swap-every-two-nodes-in-a-linked-list","tag-swapping-two-nodes-in-a-singly-linked-list","tag-unsorted-linked-list","tag-write-a-method-to-add-two-polynomial-equations-using-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27667","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=27667"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27667\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31285"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}