forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
62 lines (53 loc) · 1.25 KB
/
Copy pathCircularLinkedList.java
File metadata and controls
62 lines (53 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class CircularLinkedList extends LinkedList {
//InsertAtHead would be same as normal LinkedList
public int length(){
Node temp = this.head;
int cnt = 0;
while(temp.next != this.head){
temp = temp.next;
cnt++;
}
return cnt;
}
public void print(){
Node temp = this.head;
while(temp.next != this.head){
System.out.print(temp.data + ' ');
}
}
public void insertAtTail(int value){
Node newNode = new Node(value);
Node temp = this.head;
while(temp.next != this.head){
temp = temp.next;
}
temp.next = newNode;
newNode.next = this.head;
}
public void insertAfterNode(Node n,int value){
Node newNode = new Node(value);
Node temp = this.head;
while(temp.next != this.head){
if(temp == n){
temp.next = newNode;
newNode.next = temp.next.next;
}
}
}
public static CircularLinkedList Concat(CircularLinkedList l1, CircularLinkedList l2){
CircularLinkedList l3 = new CircularLinkedList();
l3.head = l1.head;
Node temp = l1.head;
while(temp.next != l1.head){
temp = temp.next;
}
temp.next = l2.head;
temp = temp.next;
while(temp.next != l2.head){
temp = temp.next;
}
temp.next = l1.head;
return l3;
}
//Delete by value and delete by pos would be same as normal LinkedList
}