Insert node at beginning of circular linked list
Circular linked list, last node next pointer are not NULL. That are contain the reference of first node of linked list. That is difference of single and circular linked list. Basically we can choose two variant to make a circular single linked list. First only one head pointer. And another is two pointers head and tail.
First Approach : That is very simple and similar to single linked list. And inserting a new node to beginning of this linked list this are need O(1) time. But the properties of circular linked list, always last node next pointer hold the address of first node of linked list.
So finding last node and provide next pointer reference to new head this process are tack O(n) time. So overall time complexity of first approach is O(n).
For example, suppose we are inserted the following (8,7,6,5,4,3,2,1) node in a sequence.

Here given code implementation process.
-
1) Insert node at beginning of circular linked list in c
2) Insert node at beginning of circular linked list in java
3) Insert node at beginning of circular linked list in c++
4) Insert node at beginning of circular linked list in c#
5) Insert node at beginning of circular linked list in php
6) Insert node at beginning of circular linked list in python
7) Insert node at beginning of circular linked list in ruby
8) Insert node at beginning of circular linked list in golang
9) Insert node at beginning of circular linked list in vb.net
10) Insert node at beginning of circular linked list in node js
11) Insert node at beginning of circular linked list in typescript
12) Insert node at beginning of circular linked list in scala
13) Insert node at beginning of circular linked list in swift
14) Insert node at beginning of circular linked list in kotlin
Second approach: Using of two pointers head and tail to hold the references of first and last linked list node. this process are no need to find last node so time complexity of this approach is O(1).

-
1) Effectively insert node at beginning of circular linked list in c
2) Effectively insert node at beginning of circular linked list in java
3) Effectively insert node at beginning of circular linked list in c++
4) Effectively insert node at beginning of circular linked list in golang
5) Effectively insert node at beginning of circular linked list in c#
6) Effectively insert node at beginning of circular linked list in vb.net
7) Effectively insert node at beginning of circular linked list in php
8) Effectively insert node at beginning of circular linked list in node js
9) Effectively insert node at beginning of circular linked list in typescript
10) Effectively insert node at beginning of circular linked list in python
11) Effectively insert node at beginning of circular linked list in ruby
12) Effectively insert node at beginning of circular linked list in scala
13) Effectively insert node at beginning of circular linked list in swift
14) Effectively insert node at beginning of circular linked list in kotlin
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment