Insert a node at the head of a linked list

  • + 0 comments

    My Java solution with o(1) time complexity and o(1) space complexity:

    static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {
            SinglyLinkedListNode newHead = new SinglyLinkedListNode(data);
            newHead.next = llist;
            return newHead;
        }