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->2->3->4->5, then the function should change the list to 5->1->2->3->4.

Algorithm:
Traverse 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.
i) Make second last as last (secLast->next = NULL).
ii) Set next of last as head (last->next = *head_ref).
iii) Make last as head ( *head_ref = last)

C Programming:

[pastacode lang=”c” manual=”%2F*%20C%20Program%20to%20move%20last%20element%20to%20front%20in%20a%20given%20linked%20list%20*%2F%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20A%20linked%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node%20*next%3B%0A%7D%3B%0A%20%0A%2F*%20We%20are%20using%20a%20double%20pointer%20head_ref%20here%20because%20we%20change%0A%20%20%20head%20of%20the%20linked%20list%20inside%20this%20function.*%2F%0Avoid%20moveToFront(struct%20node%20**head_ref)%0A%7B%0A%20%20%20%20%2F*%20If%20linked%20list%20is%20empty%2C%20or%20it%20contains%20only%20one%20node%2C%0A%20%20%20%20%20%20then%20nothing%20needs%20to%20be%20done%2C%20simply%20return%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL%20%7C%7C%20(*head_ref)-%3Enext%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F*%20Initialize%20second%20last%20and%20last%20pointers%20*%2F%0A%20%20%20%20struct%20node%20*secLast%20%3D%20NULL%3B%0A%20%20%20%20struct%20node%20*last%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F*After%20this%20loop%20secLast%20contains%20address%20of%20second%20last%0A%20%20%20%20node%20and%20last%20contains%20address%20of%20last%20node%20in%20Linked%20List%20*%2F%0A%20%20%20%20while%20(last-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20secLast%20%3D%20last%3B%0A%20%20%20%20%20%20%20%20last%20%3D%20last-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Set%20the%20next%20of%20second%20last%20as%20NULL%20*%2F%0A%20%20%20%20secLast-%3Enext%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20Set%20next%20of%20last%20as%20head%20node%20*%2F%0A%20%20%20%20last-%3Enext%20%3D%20*head_ref%3B%0A%20%0A%20%20%20%20%2F*%20Change%20the%20head%20pointer%20to%20point%20to%20last%20node%20now%20*%2F%0A%20%20%20%20*head_ref%20%3D%20last%3B%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Function%20to%20add%20a%20node%20at%20the%20begining%20of%20Linked%20List%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%20%0A%2F*%20Function%20to%20print%20nodes%20in%20a%20given%20linked%20list%20*%2F%0Avoid%20printList(struct%20node%20*node)%0A%7B%0A%20%20%20%20while(node%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20node-%3Edata)%3B%0A%20%20%20%20%20%20%20%20node%20%3D%20node-%3Enext%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Druver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20struct%20node%20*start%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5%20*%2F%0A%20%20%20%20push(%26start%2C%205)%3B%0A%20%20%20%20push(%26start%2C%204)%3B%0A%20%20%20%20push(%26start%2C%203)%3B%0A%20%20%20%20push(%26start%2C%202)%3B%0A%20%20%20%20push(%26start%2C%201)%3B%0A%20%0A%20%20%20%20printf(%22%5Cn%20Linked%20list%20before%20moving%20last%20to%20front%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20moveToFront(%26start)%3B%0A%20%0A%20%20%20%20printf(%22%5Cn%20Linked%20list%20after%20removing%20last%20to%20front%5Cn%22)%3B%0A%20%20%20%20printList(start)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

Output:

 Linked list before moving last to front 
1 2 3 4 5 
 Linked list after removing last to front 
5 1 2 3

Time Complexity: O(n) where n is the number of nodes in the given Linked List.