diff --git a/DataStructures/Lists/SinglyLinkedList b/DataStructures/Lists/SinglyLinkedList new file mode 100644 index 000000000..0b5181303 --- /dev/null +++ b/DataStructures/Lists/SinglyLinkedList @@ -0,0 +1,101 @@ +package LinkedList; +import java.util.*; +import java.lang.*; +import java.io.*; +class LinkedList { + private class Node{ + int data; + Node next; + + Node(int data) { + this.data = data; + this.next = null; + } + } + public Node head = null; + public Node tail = null; + private int size=0; + + public void addLast(int data) { + Node newNode = new Node(data); + + if(this.head == null) { + this.head = newNode; + this.tail = newNode; + this.size++; + } + else { + this.tail.next = newNode; + this.tail = newNode; + this.size++; + } + } + + + public void display() { + Node current = this.head; + if(this.head == null) { + return; + } + while(current != null) { + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + public void formLL2(LinkedList LL1) { + Node current=LL1.head; + while(current.next!=null&¤t.next.next!=null) { + int sum=current.data+current.next.next.data; + this.addLast(sum); + current=current.next.next; + } + } + public void formLL3(LinkedList LL1) { + Node current=LL1.head.next; + while(current.next!=null&¤t.next.next!=null) { + int sum=current.data+current.next.next.data; + this.addLast(sum); + current=current.next.next; + } + } + public Node mid() { + Node slow=this.head; + Node fast=this.head; + while(fast.next!=null && fast.next.next!=null) { + slow=slow.next; + fast=fast.next.next; + } + return slow; + } + public Node midValue() { + int sum=this.head.data+this.tail.data; + Node mid=new Node(sum); + return mid; + } + public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) { + Node LL1mid=LL1.mid(); + Node currentLL1=LL1.head; + Node currentLL2=LL2.head; + Node currentLL3=LL3.head; + while(currentLL1!=null) { + this.addLast(currentLL1.data); + + if(currentLL2!=null) { + this.addLast(currentLL2.data); + currentLL2=currentLL2.next; + }else if(currentLL1.equals(LL1mid)) { + this.addLast(MID.data); + } + else if(currentLL2==null&¤tLL3!=null) { + this.addLast(currentLL3.data); + currentLL3=currentLL3.next; + } + currentLL1=currentLL1.next; + } + } + public void Size() { + System.out.println(this.size); + } + }