forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListAddnFun.java
More file actions
153 lines (131 loc) · 4.02 KB
/
Copy pathListAddnFun.java
File metadata and controls
153 lines (131 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package DataStructures.Lists;
/*
* This class implements a SinglyLinked List.
* A linked list is similar to an array, it hold values.
* However, links in a linked list do not have indexes. With
* a linked list you do not need to predetermine it's size as
* it grows and shrinks as it is edited.
*it has functions called mid that gives node at mid
* in addn to linked list there is algo that
* construct a linked list with alternate sums of linked list
* and added to new one and add mid value
* i.e sum of first and last value of inital list
Test Case:
LinkedList LL1 = new LinkedList();
Scanner scn=new Scanner(System.in);
int numNodes=scn.nextInt();
for(int i=0;i<2*numNodes;i++) {
LL1.addLast(scn.nextInt());
}
LL1.display();
LinkedList LL2=new LinkedList();
LL2.formLL2(LL1);
LL2.display();
LinkedList LL3=new LinkedList();
LL3.formLL3(LL1);
LL3.display();
Node MID=LL1.midValue();
System.out.println(MID.data);
LinkedList updLL1=new LinkedList();
updLL1.formRes(LL1,LL2,LL3,MID);
updLL1.display();
updLL1.size();
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class LinkedList {
private class Node{
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;
private Node tail;
private int size;
public LinkedList() {
head = null;
tail = null;
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);
}
}