0

I came across appending elements to array from left. and it has two solution

Solution 1:

List = [2,3,4,5,6]
List.insert(0,1) # Using insert 
List = [1,2,3,4,5,6]

Solution 2:

List = [2,3,4,5,6]
[1] + List # Concatenation of array
List = [1,2,3,4,5,6]

I'm new to Python So please can any one explain the time complexity of both solution according to me both solution takes O(n) am I right or wrong?

Rohit s
  • 11
  • 1
  • a similar question has been asked here: https://stackoverflow.com/questions/21939652/insert-at-first-position-of-a-list-in-python – D.L Mar 03 '22 at 19:45
  • The `some_list.insert(0, x)` form is the most common. Whenever you see it though, it may be time to consider using a collections.deque instead of a list. Prepending to a deque runs in constant time. Prepending to a list runs in linear time. – D.L Mar 03 '22 at 19:48

0 Answers0