-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayLL.java
More file actions
43 lines (33 loc) · 1.19 KB
/
Copy pathdisplayLL.java
File metadata and controls
43 lines (33 loc) · 1.19 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
//Question: Display a Linked List in various way
package LinkedList;
public class displayLL {
public static class Node { // Making new class named "Node"
int val; // Has part (Data)
Node next; // Has part (Next node reference)
}
public static void main(String[] args) {
// Making 4 new "Node Objects" (a, b, c, d)
Node a = new Node();
Node b = new Node();
Node c = new Node();
Node d = new Node();
// Assigning values and linking
a.val = 10;
a.next = b;
b.val = 20;
b.next = c;
c.val = 30;
c.next = d;
d.val = 40;
Node temp = a ; // shallow copy (Same as pointer in c language) : Does not create a new memory location, it is just an another name of the same meory location of a
// first element = a = head
// for (int i = 0; i <= 4 ; i++) { // Method 1
// System.out.println(temp.val);
// temp = temp.next ;
// }
while(temp != null) { // Method 2
System.out.println(temp.val);
temp = temp.next ;
}
}
}