Circular Singly Linked List | InsertionWe have discussed Singly and Circular Linked List in the following post: Show Why Circular? In a singly linked list, for accessing any node of the linked list, we start traversing from the first node. If we are at any node in the middle of the list, then it is not possible to access nodes that precede the given node. This problem can be solved by slightly altering the structure of a singly linked list. In a singly linked list, the next part (pointer to next node) is NULL. If we utilize this link to point to the first node, then we can reach the preceding nodes. Refer to this for more advantages of circular linked lists. In this post, the implementation and insertion of a node in a Circular Linked List using a singly linked list are explained. Implementation The pointer last points to node Z and last -> next points to node P. Why have we taken a pointer that points to the last node instead of the first node? Insertion
Insertion in an empty List After inserting node T, After insertion, T is the last node, so the pointer last points to node T. And Node T is the first and the last node, so T points to itself. C++
Java
Python3
C#
Javascript
After insertion, Function to insert nodes at the beginning of the list, C++
Java
Python3
C#
Javascript
After insertion, Function to insert a node at the end of the List C++
Java
Python3
C#
Javascript
After searching and insertion, Function to insert a node at the end of the List, C++
Java
Python3
C#
Javascript
The following is a complete program that uses all of the above methods to create a circular singly linked list. C++
Java
Python3
C#
Javascript
Output: 2 4 6 8 10 12This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Article Tags :
Linked List
circular linked list Practice Tags :
Linked List circular linked list Sorted insert for circular linked listDifficulty Level: Rookie Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Algorithm: C++
C
Java
Python3
C#
Javascript
Output: 1 2 11 12 56 90 Time Complexity: O(n) where n is the number of nodes in the given linked list. Case 2 of the above algorithm/code can be optimized. To implement the suggested change we need to modify case 2 to follow. C
Java
Python3
C#
Javascript
Please write comments if you find the above code/algorithm incorrect, or find other ways to solve the same problem.
Article Tags :
Linked List
Amazon circular linked list Microsoft Zoho Practice Tags :
Zoho Amazon Microsoft Linked List circular linked list Data Structures Questions – Set 10By Aruna- AffairsCloud YouTube Channel - Click Here AffairsCloud APP Click Here Dear Aspirants,
AffairsCloud Recommends Oliveboard Mock Test
AffairsCloud Ebook - Support Us to Grow
Govt Jobs by Category
Bank Jobs Notification
Previous articleCurrent Affairs Today – December 22 2015 Next articleAptitude Questions: Probability Set 7 |