简体   繁体   中英

Verifying Big-O for 3 Loops involving LinkedList<String>

Have an exam in about 7 hours and have this question on the practice exam our instructor gave us. I'm HORRID at algorithm analysis because I haven't had enough practice yet, and the instructor didn't explain it too well.

Below is the question, and to the right of each question is what I think the order of the loops are. Here are the reasons I have for the chosen orders:

a.) each time get() is called, searching has to be begin at the front or rear of the list (since a LinkedList is a double-linked list. We didn't really discuss a LinkedList, we only created our own DLL and SLL, so my familiarity with the LinkedList isn't all that great).

b.) The call to hasNext is O(1) and inside the while loop only advances the iterator from it's current location (doesn't have to start at the front each time). Thus, it only transverses the list once.

c.) Same as b since the for-each loop works exactly the same way the code in b works. 循环

Anyways, hoping someone can confirm these for me, and if they are incorrect, tell me why.

Thank you.

They are correct.! with the same reasons as you mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM