In the below snippet, I made changes to the 'current' object, which is a copy of 'head' object. However, changes get reflected to the global head object.
class Node
{
public Node next; //pointer
public Object data; //data
public Node(Object d)
{
data = d;
}
}
public class CustomLinkedList
{
private Node head = new Node(null); //head of linked list
public void Add(Object data)
{
Node newNode = new Node(data);
Node current = head;
while (current.next != null)
{
current = current.next;
}
current.next = newNode;
}
}