-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLL_SumPairs.java
More file actions
58 lines (55 loc) · 1.49 KB
/
DoublyLL_SumPairs.java
File metadata and controls
58 lines (55 loc) · 1.49 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
package Codes;
import java.util.Scanner;
public class DoublyLL_SumPairs {
static Node head;
static class Node{
int data;
Node prev, next;
Node(int d){
data = d;
}
}
static void append(int data){
Node newNode = new Node(data);
Node last = head;
if (head == null){
newNode.prev = null;
head = newNode;
return;
}
while (last.next != null){
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
public static void sumPairs(int sum){
Node one = null, two = null;
one = head;
while (one != null && one.next != null){
two = one;
while (two.next != null){
if (one.data + two.next.data == sum){
System.out.println("( "+one.data+", "+two.next.data+" )");
two.next = two.next.next;
}
else {
two = two.next;
}
}
one = one.next;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter elements in ascending order");
int element = sc.nextInt();
while (element != -1){
append(element);
element = sc.nextInt();
}
System.out.println("enter sum");
int sum = sc.nextInt();
sumPairs(sum);
}
}