Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions InsertNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


// Linked List Class
class LinkedList
{
// Head of list
Node head;

// Node Class
class Node
{
int data;
Node next;

// Constructor to create
// a new node
Node(int d)
{
data = d;
next = null;
}
}
}

public void insertAfter(Node prev_node,
int new_data)
{
// 1. Check if the given Node is null
if (prev_node == null)
{
System.out.println(
"The given previous node cannot be null");
return;
}

/* 2. Allocate the Node &
3. Put in the data*/
Node new_node = new Node(new_data);

// 4. Make next of new Node as next
// of prev_node
new_node.next = prev_node.next;

// 5. make next of prev_node as new_node
prev_node.next = new_node;
}