Linked List 1
Linked List 1
*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
System.out.print("LinkedList: ");
// Traverse through the LinkedList
while (currNode != null) {
// Print the data at current node
System.out.print(currNode.data + " ");
// Go to next node
currNode = currNode.next;
}
}
// **************MAIN METHOD**************
//
// ******INSERTION******
//
LinkedList: 1 2 3 4 5 6 7 8
Deletion By KEY
To be done:
Given a �key�, delete the first occurrence of this key in linked list.
How to do it:
To delete a node from linked list, do following steps.
linkedlist_deletion
filter_none
edit
play_arrow
brightness_4
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
// **************DELETION BY KEY**************
//
// CASE 1:
// If head node itself holds the key to be deleted
//
// CASE 2:
// If the key is somewhere other than at head
//
//
// CASE 3: The key is not present
//
// **************MAIN METHOD**************
//
// ******INSERTION******
//
//
// ******DELETION BY KEY******
//
LinkedList: 1 2 3 4 5 6 7 8
1 found and deleted
LinkedList: 2 3 4 5 6 7 8
4 found and deleted
LinkedList: 2 3 5 6 7 8
10 not found
LinkedList: 2 3 5 6 7 8
Deletion At Position
To be done:
Given a �position�, delete the node at this position from the linked list.
How to do it:
The steps to do it are as follows:
linkedlist_deletion
filter_none
edit
play_arrow
brightness_4
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
System.out.print("LinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println();
}
//
// CASE 1:
// If index is 0, then head node itself is to be deleted
//
// CASE 2:
// If the index is greater than 0 but less than the size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
// Since the currNode is the required position
// Unlink currNode from linked list
prev.next = currNode.next;
// **************MAIN METHOD**************
//
// ******INSERTION******
//
//
// ******DELETION AT POSITION******
//
LinkedList: 1 2 3 4 5 6 7 8
0 position element deleted
LinkedList: 2 3 4 5 6 7 8
2 position element deleted
LinkedList: 2 3 5 6 7 8
10 position element not found
LinkedList: 2 3 5 6 7 8
All Operations
edit
play_arrow
brightness_4
import java.io.*;
int data;
Node next;
// Constructor
Node(int d)
{
data = d;
next = null;
}
}
// **************INSERTION**************
// **************TRAVERSAL**************
System.out.print("\nLinkedList: ");
// Go to next node
currNode = currNode.next;
}
System.out.println("\n");
}
// **************DELETION BY KEY**************
//
// CASE 2:
// If the key is somewhere other than at head
//
//
// CASE 3: The key is not present
//
// **************DELETION AT A POSITION**************
//
// CASE 1:
// If index is 0, then head node itself is to be deleted
//
// CASE 2:
// If the index is greater than 0 but less than the size of LinkedList
//
// The counter
int counter = 0;
if (counter == index) {
// Since the currNode is the required position
// Unlink currNode from linked list
prev.next = currNode.next;
// **************MAIN METHOD**************
//
// ******INSERTION******
//
//
// ******DELETION BY KEY******
//
//
// ******DELETION AT POSITION******
//
LinkedList: 1 2 3 4 5 6 7 8
LinkedList: 2 3 4 5 6 7 8
LinkedList: 2 3 5 6 7 8
10 not found
LinkedList: 2 3 5 6 7 8
LinkedList: 3 5 6 7 8
LinkedList: 3 5 7 8
LinkedList: 3 5 7 8
Recommended Posts:
RishabhPrabhu
Technical Content Engineer
If you like GeeksforGeeks and would like to contribute, you can also write an
article using contribute.geeksforgeeks.org or mail your article to
[email protected]. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the
"Improve Article" button below.
thumb_up
1
1
Based on 1 vote(s)
Please write to us at [email protected] to report any issue with the
above content.
Post navigation
Previous
first_page Deque addFirst() method in Java with Examples
Next
last_page
Recursive Approach to find nth node from the end in the linked list
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share
the link here.
Related Articles
Advertise Here