Question

Can a linked list ever have an O(n^2) behavior? For remove, add, and search as well?...

Can a linked list ever have an O(n^2) behavior? For remove, add, and search as well?

Can an array list have an O(n^2) behavior as well?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Both Arrays and Linked List can be used to store linear data of similar types, but they both have some advantages and disadvantages over each other.

40 55 63 172268 8997 89 0 1234 568Array Indice <- Array Indices Array Length 9 First Index 0 Last Index 8

LinkedList:-

Following are the points in favour of Linked Lists:.

  1. The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, upper limit is rarely reached.
  2. Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.

For example:, suppose we maintain a sorted list of IDs in an array id[].

id[] = [1000, 1010, 1050, 2000, 2040, …..].

And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).

Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.

So Linked list provides following two advantages over arrays:

  1. Dynamic size
  2. Ease of insertion/deletion

Linked lists have following drawbacks:

  1. Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists
  2. Extra memory space for a pointer is required with each element of the list.
  3. Arrays have better cache locality that can make a pretty big difference in performance.

Coming to Add a node in linkedlist:

methods to insert a new node in linked list are discussed. A node can be added in three ways

  1. At the front of the linked list
  2. After a given node.
  3. At the end of the linked list.

At the front of the linkedlist:-

The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node.

Time complexity is O(1).

At a given Node:-

We are given pointer to a node, and the new node is inserted after the given node.

Time complexity is O(1) .

At the End of the linked list:

The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.

Time complexity of append is O(n) where n is the number of nodes in linked list. Since there is a loop from head to end, the function does O(n) work.

So,Time complexity is O(n),n-No.of nodes in a linkedlist.

Remove Node from linkedlist:

It can also done in three ways as adding node in the linked list.

  1. At Front of the linkedlist    Time complexity -->O(1)
  2. At given node                   Time Complexity -->O(givne node)--given node-->position
  3. At the end                       Time Complexity -->O(n) where n is no of nodes in a linkedlist

Searching:-

Here,we can search through the all linked list,And If find return True else False

For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then function should return false. If key to be searched is 14, then the function should return true.

So A linked list can typically only be accessed via its head node. From there you can only traverse from node to node until you reach the node you seek. Thus access is O(n).

Searching for a given value in a linked list similarly requires traversing all the elements until you find that value. Thus search is O(n).

Coming to arrays:

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).

Here, Array size is fixed before taking inputs into the array.And So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage, and in practical uses, upper limit is rarely reached.

Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.

Add Element into the array    ----->Time Complexity O(1) Just add element into the array.

Delete Element in the array   ----->Time Complexity O(N) where N is Array size.

Search Element in the array ----->Time Complexity O(N) where N is Array size.

Comparision between ArrayList and LinkedList:-

  1. Insertions are easy and fast in LinkedList as compared to ArrayList because there is no risk of resizing array and copying content to new array if array gets full which makes adding into ArrayList of O(n) in worst case, while adding is O(1) operation in LinkedList. ArrayList also needs to be update its index if you insert something anywhere except at the end of array.
  2. Removal also better in LinkedList than ArrayList due to same reasons as insertion.
  3. LinkedList has more memory overhead than ArrayList because in ArrayList each index only holds actual object (data) but in case of LinkedList each node holds both data and address of next and previous node
  4. Both LinkedList and ArrayList require O(n) time to find if an element is present or not. However we can do Binary Search on ArrayList if it is sorted and therefore can search in O(Log n) time.

Thank you..!!!!

Add a comment
Know the answer?
Add Answer to:
Can a linked list ever have an O(n^2) behavior? For remove, add, and search as well?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • for java 4. Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tre...

    for java 4. Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very...

  • You implemented a stack as a singly linked list (you add at the head and remove...

    You implemented a stack as a singly linked list (you add at the head and remove from the head). Perform the following operations on the stack: push(50), push(90), push(30), push(53), push(52), pop(), push(51), pop(), pop(), push(100), and push(15). After all the operations are performed, draw the resulting linked list. Indicate which node is the top of the stack in the resulting linked list.

  • 1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

    1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...

  • A skip list is a data structure that allows Oflog n) search complexity as well as O(log n)insertion complexity within an ordered sequence of n elements. A skip list is built in layers. Each layer is...

    A skip list is a data structure that allows Oflog n) search complexity as well as O(log n)insertion complexity within an ordered sequence of n elements. A skip list is built in layers. Each layer is a linked list. The bottom layer is an ordinary ordered linked list. Each higher layer acts as an "express lane" for the lists below, where an element in layer L appears in layer L+1 (higher layer) with some fixed probability p. A search for...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create...

    Instead of using a linked list to resolve collisions, as in separate chaining, use a binary search tree. That is, create a hash table that is an array of trees. To display a small tree-based hash table, you could use an inorder traversal of each tree. The advantage of a tree over a linked list is that it can be searched in O(logN) instead of O(N) time. This time savings can be a significant advantage if very high load factors...

  • True/False Question 1. A Linked List will have no limited size. You may add or insert...

    True/False Question 1. A Linked List will have no limited size. You may add or insert as many elements as needed. True False 2. Linked List search is a linear search. Therefore it will be slow. True False 3. A JPanel can contain zero or more JPanels. True False 4. Users can interact with JLabel component. True False 5. This code is to add a panel to the south region of a JFrame JPanel panel = new JPanel (); frame.add...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • ...this is a subpart of the previous questions 2) Find the linked list for the starting...

    ...this is a subpart of the previous questions 2) Find the linked list for the starting node given by the user (it'll be easy to do so since you have them stored in a dictionary and you don't need to search for that element using sequential search) 3) Queue all of the elements in the linked list 4) Dequeue one element at a time, visit that element and do step 2 (find the linked list for that element in the...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT