-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListLastP.java
More file actions
53 lines (48 loc) · 1.12 KB
/
LinkedListLastP.java
File metadata and controls
53 lines (48 loc) · 1.12 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
44
45
46
47
48
49
50
51
52
53
public class LinkedListLastP {
public class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;
public void addAtLast(int data) {
Node newNode = new Node(data);
if (head == null) {
newNode.next = null;
head = newNode;
return;
}
Node i;
i = head;
while (i != null) {
if (i.next == null) {
i.next = newNode;
return;
}
i = i.next;
}
}
public void printList() {
Node i;
i = head;
while (i != null) {
System.out.print(i.data);
if (i.next != null) {
System.out.print(" -> ");
}
i = i.next;
}
}
public static void main(String[] args) {
LinkedListLastP ll = new LinkedListLastP();
ll.addAtLast(0);
// ll.printList();
ll.addAtLast(1);
// ll.printList();
ll.addAtLast(2);
ll.printList();
}
}