|
| 1 | +package DataStructures.Lists; |
| 2 | + |
| 3 | +/* |
| 4 | + * This class implements a SinglyLinked List. |
| 5 | + * A linked list is similar to an array, it hold values. |
| 6 | + * However, links in a linked list do not have indexes. With |
| 7 | + * a linked list you do not need to predetermine it's size as |
| 8 | + * it grows and shrinks as it is edited. |
| 9 | + *it has functions called mid that gives node at mid |
| 10 | + * in addn to linked list there is algo that |
| 11 | + * construct a linked list with alternate sums of linked list |
| 12 | + * and added to new one and add mid value |
| 13 | + * i.e sum of first and last value of inital list |
| 14 | + |
| 15 | + Test Case: |
| 16 | + |
| 17 | + |
| 18 | + LinkedList LL1 = new LinkedList(); |
| 19 | + Scanner scn=new Scanner(System.in); |
| 20 | + int numNodes=scn.nextInt(); |
| 21 | + for(int i=0;i<2*numNodes;i++) { |
| 22 | + LL1.addLast(scn.nextInt()); |
| 23 | + } |
| 24 | + LL1.display(); |
| 25 | + LinkedList LL2=new LinkedList(); |
| 26 | + LL2.formLL2(LL1); |
| 27 | + LL2.display(); |
| 28 | + LinkedList LL3=new LinkedList(); |
| 29 | + LL3.formLL3(LL1); |
| 30 | + LL3.display(); |
| 31 | + Node MID=LL1.midValue(); |
| 32 | + System.out.println(MID.data); |
| 33 | + LinkedList updLL1=new LinkedList(); |
| 34 | + updLL1.formRes(LL1,LL2,LL3,MID); |
| 35 | + updLL1.display(); |
| 36 | + updLL1.Size(); |
| 37 | + |
| 38 | + */ |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +import java.util.*; |
| 43 | +import java.lang.*; |
| 44 | +import java.io.*; |
| 45 | +class LinkedList { |
| 46 | + private class Node{ |
| 47 | + int data; |
| 48 | + Node next; |
| 49 | + |
| 50 | + Node(int data) { |
| 51 | + this.data = data; |
| 52 | + this.next = null; |
| 53 | + } |
| 54 | + } |
| 55 | + public Node head = null; |
| 56 | + public Node tail = null; |
| 57 | + private int size=0; |
| 58 | + |
| 59 | + public void addLast(int data) { |
| 60 | + Node newNode = new Node(data); |
| 61 | + |
| 62 | + if(this.head == null) { |
| 63 | + this.head = newNode; |
| 64 | + this.tail = newNode; |
| 65 | + this.size++; |
| 66 | + } |
| 67 | + else { |
| 68 | + this.tail.next = newNode; |
| 69 | + this.tail = newNode; |
| 70 | + this.size++; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + public void display() { |
| 76 | + Node current = this.head; |
| 77 | + if(this.head == null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + while(current != null) { |
| 81 | + System.out.print(current.data + " "); |
| 82 | + current = current.next; |
| 83 | + } |
| 84 | + System.out.println(); |
| 85 | + } |
| 86 | + |
| 87 | + public void formLL2(LinkedList LL1) { |
| 88 | + Node current=LL1.head; |
| 89 | + while(current.next!=null&¤t.next.next!=null) { |
| 90 | + int sum=current.data+current.next.next.data; |
| 91 | + this.addLast(sum); |
| 92 | + current=current.next.next; |
| 93 | + } |
| 94 | + } |
| 95 | + public void formLL3(LinkedList LL1) { |
| 96 | + Node current=LL1.head.next; |
| 97 | + while(current.next!=null&¤t.next.next!=null) { |
| 98 | + int sum=current.data+current.next.next.data; |
| 99 | + this.addLast(sum); |
| 100 | + current=current.next.next; |
| 101 | + } |
| 102 | + } |
| 103 | + public Node mid() { |
| 104 | + Node slow=this.head; |
| 105 | + Node fast=this.head; |
| 106 | + while(fast.next!=null && fast.next.next!=null) { |
| 107 | + slow=slow.next; |
| 108 | + fast=fast.next.next; |
| 109 | + } |
| 110 | + return slow; |
| 111 | + } |
| 112 | + public Node midValue() { |
| 113 | + int sum=this.head.data+this.tail.data; |
| 114 | + Node mid=new Node(sum); |
| 115 | + return mid; |
| 116 | + } |
| 117 | + public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) { |
| 118 | + Node LL1mid=LL1.mid(); |
| 119 | + Node currentLL1=LL1.head; |
| 120 | + Node currentLL2=LL2.head; |
| 121 | + Node currentLL3=LL3.head; |
| 122 | + while(currentLL1!=null) { |
| 123 | + this.addLast(currentLL1.data); |
| 124 | + |
| 125 | + if(currentLL2!=null) { |
| 126 | + this.addLast(currentLL2.data); |
| 127 | + currentLL2=currentLL2.next; |
| 128 | + }else if(currentLL1.equals(LL1mid)) { |
| 129 | + this.addLast(MID.data); |
| 130 | + } |
| 131 | + else if(currentLL2==null&¤tLL3!=null) { |
| 132 | + this.addLast(currentLL3.data); |
| 133 | + currentLL3=currentLL3.next; |
| 134 | + } |
| 135 | + currentLL1=currentLL1.next; |
| 136 | + } |
| 137 | + } |
| 138 | + public void Size() { |
| 139 | + System.out.println(this.size); |
| 140 | + } |
| 141 | + } |
0 commit comments