Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ddecef4

Browse files
author
Ram swaroop
committed
divide a single circular linked list into two halves
1 parent 0ecdc0c commit ddecef4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)