-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_creation.java
More file actions
48 lines (42 loc) · 1020 Bytes
/
linked_list_creation.java
File metadata and controls
48 lines (42 loc) · 1020 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
39
40
41
42
43
44
45
46
47
48
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
//Node creation ends here.
//Create a linked list.
public class linked_list_creation {
static Node head;
linked_list_creation(){
head=null;
}
static void printData(Node head){
Node current=head;
while (current!=null){
System.out.print(current.data+"---->");
current=current.next;
}
System.out.print("null");
}
static int sum(Node head){
int sum=0;
Node cur=head;
while (cur != null){
sum=sum +cur.data;
cur=cur.next;
}
return sum;
}
public static void main(String[] args) {
head=new Node(10);
head.next=new Node(20);
head.next.next=new Node(30);
head.next.next.next=new Node(40);
printData(head);
System.out.println();
System.out.println("sum of all elements "+sum(head));
}
}