Problem of the day
You are given the head of a linked list ‘list’ of size ‘N’ and an integer ‘newValue’.
Your task is to insert a node with the value ‘newValue’ at the beginning of the ‘list’ and return the new head of the linked list.
You must write an algorithm whose time complexity is O(1) and whose space complexity is O(1).
In the output, you will see the elements of the linked list made by you.
Input: ‘N’ = 4, ‘newValue’ = 0
‘list’ = 4 -> 2 -> 5 -> 1
Output: 0 4 2 5 1
Explanation: Linked List after inserting the newValue is 0 -> 4 -> 2 -> 5 -> 1.
The first line contains two integers, ‘N’ and ‘newValue’, denoting the size of the linked list ‘list’ and the value of the integer.
The following line contains ‘N’ integers, denoting the elements of the ‘list’.
Return the head of the new linked list.
You don't need to print anything. Just implement the given function.
4 0
4 2 5 1
0 4 2 5 1
Input: ‘N’ = 4, ‘newValue’ = 0
‘list’ = 4 -> 2 -> 5 -> 1
Output: 0 4 2 5 1
Explanation: Linked List after inserting the newValue is 0 -> 4 -> 2 -> 5 -> 1.
5 5
4 3 2 1 5
5 4 3 2 1 5
Input: ‘N’ = 5, ‘newValue’ = 5
‘list’ = 4 -> 3 -> 2 -> 1 -> 5
Output: 5 4 3 2 1 5
Explanation: Linked List after inserting the newValue is 5 -> 4 -> 3 -> 2 -> 1 -> 5.
1 <= 'N' <= 10^4
0 <= list elements <= 10^5
0 <= 'newValue' <= 10^5
Time Limit: 1-sec