Skip to content

Commit 278529e

Browse files
authored
Create SinglyLinkedList
1 parent 627115b commit 278529e

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

DataStructures/Lists/SinglyLinkedList

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package LinkedList;
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
class LinkedList {
6+
private class Node{
7+
int data;
8+
Node next;
9+
10+
Node(int data) {
11+
this.data = data;
12+
this.next = null;
13+
}
14+
}
15+
public Node head = null;
16+
public Node tail = null;
17+
private int size=0;
18+
19+
public void addLast(int data) {
20+
Node newNode = new Node(data);
21+
22+
if(this.head == null) {
23+
this.head = newNode;
24+
this.tail = newNode;
25+
this.size++;
26+
}
27+
else {
28+
this.tail.next = newNode;
29+
this.tail = newNode;
30+
this.size++;
31+
}
32+
}
33+
34+
35+
public void display() {
36+
Node current = this.head;
37+
if(this.head == null) {
38+
return;
39+
}
40+
while(current != null) {
41+
System.out.print(current.data + " ");
42+
current = current.next;
43+
}
44+
System.out.println();
45+
}
46+
47+
public void formLL2(LinkedList LL1) {
48+
Node current=LL1.head;
49+
while(current.next!=null&&current.next.next!=null) {
50+
int sum=current.data+current.next.next.data;
51+
this.addLast(sum);
52+
current=current.next.next;
53+
}
54+
}
55+
public void formLL3(LinkedList LL1) {
56+
Node current=LL1.head.next;
57+
while(current.next!=null&&current.next.next!=null) {
58+
int sum=current.data+current.next.next.data;
59+
this.addLast(sum);
60+
current=current.next.next;
61+
}
62+
}
63+
public Node mid() {
64+
Node slow=this.head;
65+
Node fast=this.head;
66+
while(fast.next!=null && fast.next.next!=null) {
67+
slow=slow.next;
68+
fast=fast.next.next;
69+
}
70+
return slow;
71+
}
72+
public Node midValue() {
73+
int sum=this.head.data+this.tail.data;
74+
Node mid=new Node(sum);
75+
return mid;
76+
}
77+
public void formRes(LinkedList LL1,LinkedList LL2,LinkedList LL3,Node MID) {
78+
Node LL1mid=LL1.mid();
79+
Node currentLL1=LL1.head;
80+
Node currentLL2=LL2.head;
81+
Node currentLL3=LL3.head;
82+
while(currentLL1!=null) {
83+
this.addLast(currentLL1.data);
84+
85+
if(currentLL2!=null) {
86+
this.addLast(currentLL2.data);
87+
currentLL2=currentLL2.next;
88+
}else if(currentLL1.equals(LL1mid)) {
89+
this.addLast(MID.data);
90+
}
91+
else if(currentLL2==null&&currentLL3!=null) {
92+
this.addLast(currentLL3.data);
93+
currentLL3=currentLL3.next;
94+
}
95+
currentLL1=currentLL1.next;
96+
}
97+
}
98+
public void Size() {
99+
System.out.println(this.size);
100+
}
101+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy