performance - Finding the Space Complexitiy for these methods -


i'm familiar running time both of following methods o(n). however, i'm not familiar space complexity. since these methods consists of assignment statements, comparison statements, , loops, assume space complexity o(1), want make sure. thank you.

//first method  public static <e> node<e> buildlist(e[] items) {     node<e> head = null;      if (items!=null && items.length>0) {         head = new node<e> (items[0], null);         node<e> tail = head;         (int i=1; i<items.length; i++) {             tail.next = new node<e>(items[i], null);             tail = tail.next;         }     }     return head; }  //second method public static <e> int getlength(node<e> head) {     int length = 0;      node<e> node = head;     while (node!=null) {         length++;         node = node.next;      }     return length; } 

as described in this post, space complexity related amount of memory used algorithm, depending on size of input.

for instance algorithm o(1) space complexity uses fixed amount of memory, independently of input size. case of second algorithm uses few variables.

an algorithm o(n) space complexity uses amount of memory proportional input size. case of first algorithm performs 1 allocation (one new) each element of input.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -