Error
|
|
---|---|
@Ibrahim Nash | 6379 |
@blackshadows | 6329 |
@mb1973 | 5358 |
@Quandray | 5231 |
@akhayrutdinov | 5111 |
@saiujwal13083 | 4510 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3522 |
@sushant_a | 3459 |
@verma_ji | 3357 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@ritiksethi21 | 1050 |
@aroranayan999 | 807 |
@RizulBansal | 685 |
@ashishtrehan002 | 538 |
@hemantgarg923 | 528 |
@simrangoyal | 526 |
@ronaldo77 | 520 |
@thanosagain | 505 |
@anishrajan | 505 |
@ssparteek470 | 495 |
@rahul2312 | 491 |
Complete Leaderboard |
Create a link list of size N according to the given input literals. Each integer input is accompanied by an indicator which can either be 0 or 1. If it is 0, insert the integer in the beginning of the link list. If it is 1, insert the integer at the end of the link list.
Hint: When inserting at the end, make sure that you handle NULL explicitly.
Example 1:
Input:
LinkedList: 9->0->5->1->6->1->2->0->5->0
Output: 5 2 9 5 6
Explanation:
Length of Link List = N = 5
9 0 indicated that 9 should be
inserted in the beginning. Modified
Link List = 9.
5 1 indicated that 5 should be
inserted in the end. Modified Link
List = 9,5.
6 1 indicated that 6 should be
inserted in the end. Modified Link
List = 9,5,6.
2 0 indicated that 2 should be
inserted in the beginning. Modified
Link List = 2,9,5,6.
5 0 indicated that 5 should be
inserted in the beginning. Modified
Link List = 5,2,9,5,6.
Final linked list = 5, 2, 9, 5, 6.
Example 2:
Input:
LinkedList: 5->1->6->1->9->1
Output: 5 6 9
Your Task:
You only need to complete the functions insertAtBeginning() and insertAtEnd() that takes the head of link list and integer value of the data to be inserted as inputs and returns the head of the modified link list.
Expected Time Complexity: O(1) for insertAtBeginning() and O(N) for insertAtEnd().
Expected Auxiliary Space: O(1) for both.
Constraints:
1 <= N <= 104
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes