File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
src/me/ramswaroop/linkedlists Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ package me .ramswaroop .linkedlists ;
2
+
3
+ import me .ramswaroop .common .CircularSingleLinkedList ;
4
+ import me .ramswaroop .common .SingleLinkedNode ;
5
+
6
+ /**
7
+ * Created by IntelliJ IDEA.
8
+ *
9
+ * @author: ramswaroop
10
+ * @date: 6/23/15
11
+ * @time: 6:01 PM
12
+ */
13
+ public class DivideCircularListIntoTwo <E extends Comparable <E >> extends CircularSingleLinkedList <E > {
14
+
15
+ public static <E extends Comparable <E >> CircularSingleLinkedList [] divideIntoTwoHalves (CircularSingleLinkedList <E > list ) {
16
+ SingleLinkedNode <E > middleNode = list .getNode (list .size >> 1 ),
17
+ lastNode = list .getNode (list .size - 1 ),
18
+ secondHead = middleNode .next ;
19
+
20
+ lastNode .next = middleNode .next ;
21
+ middleNode .next = list .head ;
22
+
23
+ return new CircularSingleLinkedList []{getLinkedList (list .head ), getLinkedList (secondHead )};
24
+ }
25
+
26
+ public static void main (String a []) {
27
+ CircularSingleLinkedList <Integer > linkedList = new CircularSingleLinkedList <>();
28
+ linkedList .add (00 );
29
+ linkedList .add (11 );
30
+ linkedList .add (22 );
31
+ linkedList .add (33 );
32
+ linkedList .add (44 );
33
+ CircularSingleLinkedList <Integer > linkedLists [] = divideIntoTwoHalves (linkedList );
34
+ linkedLists [0 ].printList ();
35
+ linkedLists [1 ].printList ();
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments