-
Notifications
You must be signed in to change notification settings - Fork 53
/
LinkedListDeletion.java
63 lines (53 loc) · 1.43 KB
/
LinkedListDeletion.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Java_DSA.Problems.Arrays;
class Node {
int data;
Node next;
Node(int value)
{
data = value;
next = null;
}
}
public class LinkedListDeletion {
// Function to delete the head node
public static Node deleteHead(Node head)
{
// Base case if linked list is empty
if (head == null)
return null;
// Change the head pointer to the next node
// and free the original head
Node temp = head;
head = head.next;
// Free the original head (handled by garbage
// collector)
temp = null;
// Return the new head
return head;
}
public static void printList(Node head)
{
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("null");
}
public static void main(String[] args)
{
// Creating a linked list
// 1 -> 2 -> 3 -> 4 -> 5 -> null
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
System.out.println("Original list: ");
printList(head);
// Deleting the head node
head = deleteHead(head);
System.out.println(
"List after deleting the head: ");
printList(head);
}
}