Insert Node in Linked List

Singly Linked List: Fundamentals DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Insert a node at the beginning of a linked list, Linked List, Node, Pointer, Time Complexity, Space Complexity, Big O Notation, complexity analysis, pointer operations, node operations, linked list, Linked List Operations.

Given the head of a singly linked list and an integer val, insert a new node with the value val at the beginning of the list. Return the new head of the modified linked list. Input Specification: The input will consist of the head of a linked list and an integer val. Output Specification: The output should be the head of the updated linked list after inserting the new node. Example 1: Input: 0 1 2, val = 5 Output: 5 0 1 2 Explanation: The value 5 is inserted before the head of the given Linked List. Example 2: Input: 12 5 8 7, val = 100 Output: 100 12 5 8 7 Explanation: The value 100 is inserted before the head of the Linked List.