Approach for inserting a new node before a given node value in a linked list:
- Create a new node with the data value you want to insert.
- If the linked list is empty, print a message indicating that the list is empty and return.
- If the value you want to insert before is in the first node of the linked list, insert the new node before the first node and update the head property to point to the new node. Return.
- Loop through the linked list and check whether the data value of the next node is equal to the value you want to insert before.
- If the value is found, insert the new node before the node with the value and return.
- If the value is not found, print a message indicating that the value was not found.
To insert a new node before a given node value in a linked list, Here’s an example Kotlin code:
class Node(var data: Int) {
var next: Node? = null
}
class LinkedList {
var head: Node? = null
fun insertBefore(value: Int, newNode: Node) {
if (head == null) {
println("Linked list is empty")
return
}
if (head!!.data == value) {
newNode.next = head
head = newNode
return
}
var current = head
while (current?.next != null) {
if (current.next!!.data == value) {
newNode.next = current.next
current.next = newNode
return
}
current = current.next
}
println("Value '$value' not found in linked list")
}
}
// Example usage
fun main() {
val ll = LinkedList()
ll.head = Node(1)
ll.head!!.next = Node(2)
ll.head!!.next!!.next = Node(3)
val newNode = Node(4)
ll.insertBefore(2, newNode)
// The linked list should now be 1 -> 4 -> 2 -> 3
}
Here’s a step-by-step explanation of the insertBefore
method in the Kotlin code I provided earlier, which inserts a new node before a given node value in a linked list:
- The method takes two arguments:
value
, an integer value representing the node value before which we want to insert the new node, andnewNode
, aNode
object representing the new node we want to insert. - If the linked list is empty (i.e., the
head
property is null), the method prints a message indicating that the list is empty and returns. - If the value we’re looking for is in the first node of the linked list (i.e., the
data
property of thehead
node is equal tovalue
), we insert the new node before the first node by updating thenext
property ofnewNode
to point to the currenthead
, and then updating thehead
property to point tonewNode
. We then return from the method. - If the value we’re looking for is not in the first node of the linked list, we initialize a variable called
current
to point to the first node in the list (i.e., thehead
property). - We then iterate over the linked list using a while loop. The loop continues as long as
current.next
is not null (i.e., there are more nodes to traverse), and we use the null-safe call operator (?.
) to ensure we don’t get aNullPointerException
ifcurrent
is null. - Inside the loop, we check whether the
data
property of the next node in the list (i.e.,current.next!!.data
) is equal tovalue
. If it is, we have found the node before which we want to insert the new node. - We then update the
next
property ofnewNode
to point to the node aftercurrent
, which is the node with the valuevalue
. We do this by settingnewNode.next
equal tocurrent.next
. - Finally, we update the
next
property ofcurrent
to point tonewNode
, effectively insertingnewNode
into the linked list before the node with the valuevalue
. - If the value we’re looking for is not found in the linked list, we print a message indicating that the value was not found.
The time complexity of inserting a node before a given node value in a linked list is O(n), where n is the number of nodes in the linked list. This is because in the worst case scenario, we may have to traverse the entire linked list to find the node with the value we want to insert before.
The space complexity of this operation is O(1), which is constant space, since we are only creating a single new node and not using any additional data structures that would require more memory as the size of the linked list grows.