-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list_deletion_at_last.java
More file actions
42 lines (37 loc) · 1016 Bytes
/
Copy pathlinked_list_deletion_at_last.java
File metadata and controls
42 lines (37 loc) · 1016 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
//Write a program to delete a node of linked list at the endingo of the list.
public class linked_list_deletion_at_last {
static Node head;
linked_list_deletion_at_last() {
head = null;
}
static void printData(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + "---->");
curr = curr.next;
}
System.out.print("null");
}
static void deletion() {
if (head == null)
return;
if (head.next == null) {
head = null;
return;
}
Node curr = head;
while (curr.next.next != null) {
curr = curr.next;
}
curr.next = null;
}
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);
deletion();
printData(head);
System.out.println();
}
}