JaeYeopHan / Interview_Question_for_Beginner

:boy: :girl: Technical-Interview guidelines written for those who started studying programming. I wish you all the best. :space_invader:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

자료구조(Array vs Linked) 관련하여 질문을 드리고자합니다.

Seolhun opened this issue · comments

This issue is...

  • Edit typos or links
  • Inaccurate information
  • New Resources
  • Suggestions
  • Questions
  • Enhancement
  • Comments

Description

  /*** Returns the (non-null) Node at the specified element index.*/
  Node<E> node(int index) {
    // assert isElementIndex(index);
    if (index < (size >> 1)) {
      Node<E> x = first;
      for (int i = 0; i < index; i++)
          x = x.next;
      return x;
    } else {
      Node<E> x = last;
      for (int i = size - 1; i > index; i--)
          x = x.prev;
      return x;
    }
  }

Java LinkedList 코드를 보면 삭제/삽입 전 해당 인덱스를 탐색하기 위해 Binary search와 같은 중간 값을 기준으로 탐색을 합니다. 즉, 이는 시간 복잡도가 O(n)이 아닌 O(Log N)을 갖는 것이 더 적합한 의견이 아닐까 생각하는데, 어떻게 생각하는지 알고싶습니다.
그리고 좋은 글 감사합니다.

commented

@Seolhun 안녕하세요 :)
LinkedList를 구현하는 방법에는 여러 가지가 있을 수 있는데요, 보통 LinkedList의 시간복잡도를 이야기할 때는 배열을 통해 구현하는 경우를 말하기 때문에, O(n)으로 명시를 해두었습니다. 말씀하신 것처럼 탐색하기 위해 BST를 사용한다면 시간복잡도는 O(log n)이 되겠지요 ^^;;

답변이 되었는지 모르겠네요. 답변이 되었다면 해당 issue close 해주시고, 부족하다면 부족한 부분에 대한 comment 부탁드려요~

감사합니다.