-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLL_traversal.java
47 lines (43 loc) · 934 Bytes
/
LL_traversal.java
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
/**
* ip: NULL
* op:
*
* ip:
* 10 -> 5 -> 20 -> 15
* | |
* ------------------
* op: 10 5 20 15 10
*
* ip: 10
* op: 10 -> 10
*/
public class LL_traversal {
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(5);
head.next.next = new Node(20);
head.next.next.next = new Node(15);
head.next.next.next.next = head;
// creating a single node CLL
Node head2 = new Node(10);
head2.next = head2;
printList(head);
// printList(head2);
}
public static void printList(Node head)
{
if(head==null)
return;
System.out.print(head.data + "->");
for(Node r = head.next; r!=head; r = r.next)
System.out.print(r.data + "->");
System.out.print(head.data);
}
}
/**
* op:
* 10->5->20->15->10
*
* op:
* 10->10
*/