I am just learning linkedlists and trying to insert a node at the head. Here is my code:
Node Insert(Node head,int x) {
    Node tmp = new Node();
    if (head == null) {
        head = tmp;
        tmp.data = x;
    }
    tmp.data = x;
    tmp = head;
    tmp.next = head;
    return tmp;
}
I am not sure what is going wrong. My logic:
- Create a new node 'tmp'
- assign that value of 'x' and the reference to tmp would be null
- MAke tmp's next i.e. reference to the head of the linkedlist
- if head is null then tmp's data becomes head's data and tmp's reference become null
