-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSplitToTwo.java
88 lines (73 loc) · 2.59 KB
/
SplitToTwo.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Split a Circular Linked List into two halves
// If there are odd number of nodes, then first list should contain one extra.
// 1) Store the mid and last pointers of the circular linked list using tortoise and hare algorithm.(Floyd)
// 2) Make the second half circular.
// 3) Make the first half circular.
// 4) Set head (or start) pointers of the two linked lists.
// In the below implementation,
// if there are odd nodes in the given circular linked list
// then the first result list has 1 more node than the second result list.
class CircularLinkedList{
static Node head,headA,headB;
static class Node{
int data;
Node prev,next;
Node(int data){
this.data = data;
prev = next = null;
}
}
public void splitList(){
Node hare = head;
Node tortoise = head;
if(head == null) return;
/* If there are odd nodes in the circular list then
hare->next becomes head and for even nodes
hare->next->next becomes head */
while(hare.next != head && hare.next.next != head){
hare = hare.next.next;
tortoise = tortoise.next;
}
/* If there are even elements in list then move hare */
while(hare.next.next == head)
hare = hare.next;
/* Set the head pointer of first half */
headA = head;
/* Set the head pointer of second half */
if (head.next != head) {
headB = tortoise.next;
}
/* Make second half circular */
hare.next = tortoise.next;
/* Make first half circular */
tortoise.next = head;
}
void print(Node node) {
Node temp = node;
if (node != null) {
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != node);
}
}
public static void main(String[] args) {
CircularLinkedList c = new CircularLinkedList();
//Created linked list will be 12->56->2->11
c.head = new Node(12);
c.head.next = new Node(56);
c.head.next.next = new Node(2);
c.head.next.next.next = new Node(11);
c.head.next.next.next.next = c.head;
System.out.println("Original Circular Linked list ");
c.print(head);
// Split the list
c.splitList();
System.out.println("");
System.out.println("First Circular List ");
c.print(headA);
System.out.println("");
System.out.println("Second Circular List ");
c.print(headB);
}
}