-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1.txt
114 lines (89 loc) · 2.66 KB
/
q1.txt
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
1.
/**
* Created by Avsek on 5/8/2017.
*/
public class LinkedList {
Node head;
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Inserts a new Node at front of the list. */
public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void deleteNode(int position)
{
if (head == null)
return;
Node temp = head;
if (position == 0)
{
head = temp.next; // Change head
return;
}
for (int i=0; temp!=null && i<position-1; i++)
temp = temp.next;
if (temp == null || temp.next == null)
return;
Node next = temp.next.next;
temp.next = next;
}
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
System.out.println("\nCreated Linked list is: ");
llist.printList();
llist.deleteNode(1); // Delete node at position 1; position starts from 0
System.out.println("\nLinked List after Deletion at position 1: ");
llist.printList();
}
}
20.
import java.util.ArrayList;
import java.util.List;
/**
* Created by Avsek on 5/8/2017.
*/
public class SumOfMultiples {
public static void main(String[] args) {
int sumOfThree = 0;
int sumOfFive = 0;
List<Integer> listOfThree = new ArrayList<>();
List<Integer> listOfFive = new ArrayList<>();
for(int i = 3; i<1000; i=i+3){
listOfThree.add(i);
}
for (int i = 5; i<1000; i=i+5){
listOfFive.add(i);
}
for(int n: listOfThree)
sumOfThree += n;
for (int n: listOfFive)
sumOfFive += n;
System.out.println("Final Sum :" + sumOfThree + sumOfFive);
}
}