LinkedList with side nodes
class SidelyLinkedList { static Node head; static class Node { int item; Node next; Node side; Node(int item) { this.item =item; next = null; side = null; } } public static void main(String[] args) { SidelyLinkedList list = new SidelyLinkedList(); list.head = new Node(51); list.head.next = new Node(10); list.head.next.next = new Node(15); list.head.next.next.next = new Node(18); list.head.next.next.next.next= new Node(21); list.head.side = new Node(34); list.head.side.side = new Node(48); list.head.next.side = new Node(23); list.head.next.side.side = new Node(67); list.head.next.next.side = new Node(12); list.head.next.next.side.side = new Node(89); list.head.next.next.next.side = new Node(67); list.head.next.next.next.side.side = new Node(92); list.head.next.next.next.next.side = new Node(56); list.head.next.next.next.next.side.side = new Node(97); Node res1 = list.singleList(head); list.printSidelyLinkedList(res1); } private Node singleList(Node node) { if(node.next==null){ return node; } else{ Node temp = node; Node temp1= node.next; while(temp.side!= null){ temp= temp.side; } temp.side= temp1; node.next=temp1.next; return singleList(node); } } private void printSidelyLinkedList(Node head) { if(head==null){ return; } else { System.out.println(head.item +"->"); printSidelyLinkedList(head.side); } } } Output:
51->
34->
48->
10->
23->
67->
15->
12->
89->
18->
67->
92->
21->
56->
97->
No comments:
Post a Comment