Write a C function that moves first element to end 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 2->3->4->5->1.
Traverse the list till last node. Use two pointers: one to store the address of last node(last) and other for address of first node(first). After the end of loop do following operations.
i) Make head as second node (*head_ref = first->next).
ii) Set next of first as NULL (first->next = NULL).
iii) Set next of last as first ( last->next = first)
Linked list before moving first to end 1 2 3 4 5 Linked list after moving first to end 2 3 4 5 1
Time Complexity: O(n) where n is the number of nodes in the given Linked List.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Move all occurrences of an element to end in a linked list
- Move last element to front of a given Linked List | Set 2
- Move last element to front of a given Linked List
- Create new linked list from two given linked list with greater element at each node
- Move last m elements to the front of a given Linked List
- Move all zeros to the front of the linked list
- Given a linked list, reverse alternate nodes and append at the end
- Delete Nth node from the end of the given linked list
- Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
- Rearrange a linked list in to alternate first and last element
- Find the first duplicate element in the linked list
- Swap Kth node from beginning with Kth node from end in a Linked List
- Recursive Approach to find nth node from the end in the linked list
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Remove Nth node from end of the Linked List
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Program for n'th node from the end of a Linked List
- Create a linked list from two linked lists by choosing max element at each position
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.