-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist-oddnums_add.java
More file actions
38 lines (38 loc) · 967 Bytes
/
linkedlist-oddnums_add.java
File metadata and controls
38 lines (38 loc) · 967 Bytes
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
import java.util.*;
class Main{
static class Node{
int data;
Node nxt;
}
public static void insert(Node head){
Node obj = new Node();
Scanner sc = new Scanner(System.in);
obj.data = sc.nextInt();
obj.nxt = head.nxt;
head.nxt = obj;
}
public static void display(Node head){
Node p = head.nxt;
int sum = 0;
while(p!=null){
if(p.data!=0){
if((p.data%2)!=0){
sum = sum+p.data;
}
}
p = p.nxt;
}
System.out.print("odd nums addition:"+sum);
}
public static void main(String [] args){
Node head = new Node();
head.nxt = null;
System.out.print("enter no.of elements:");
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
for(int i=0;i<n;i++){
insert(head);
}
display(head);
}
}