Skip to content

Added Important Algos #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 25, 2020
Prev Previous commit
Next Next commit
Create SinglyLinkedList
  • Loading branch information
Ritik2604 authored May 23, 2020
commit 278529eb8eb28631e84d9d780352e995d179bc8c
101 changes: 101 additions & 0 deletions DataStructures/Lists/SinglyLinkedList
Original file line number Diff line number Diff line change
@@ -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&&current.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&&current.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&&currentLL3!=null) {
this.addLast(currentLL3.data);
currentLL3=currentLL3.next;
}
currentLL1=currentLL1.next;
}
}
public void Size() {
System.out.println(this.size);
}
}