Skip to content

Commit d7a2b7f

Browse files
committed
feat: DummyDoublyLinkedList 데이터 제거 예제 코드 작성
1 parent a16d3ae commit d7a2b7f

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

yoonexample/src/main/java/list/DummyDoublyLinkedList.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,41 @@ public E get(int index) {
9797

9898
@Override
9999
public E remove(int index) {
100-
return null;
100+
if (isEmpty()) {
101+
return null;
102+
}
103+
104+
Node<E> cur;
105+
106+
// 절반을 나눠서 가까운 부분에서 접근하도록 합니다.
107+
if (index < (this.size / 2) + 1) { // 중간 값보다 작은 경우 앞에서부터 탐색합니다.
108+
cur = this.head.next;
109+
110+
for (int i = 0; i < index; i++) {
111+
if (cur.next == null) {
112+
return null;
113+
}
114+
cur = cur.next;
115+
}
116+
} else {
117+
cur = this.tail;
118+
int reversedIndex = this.size - index;
119+
120+
if (reversedIndex < 1) { // 현재 조회 가능한 index보다 조회한 index가 큰 경우
121+
return null;
122+
}
123+
for (int i = 0; i < reversedIndex; i++) {
124+
if (cur.prev == null) {
125+
return null;
126+
}
127+
cur = cur.prev;
128+
}
129+
}
130+
cur.prev.next = cur.next;
131+
cur.next.prev = cur.prev;
132+
this.size--;
133+
134+
return cur.data;
101135
}
102136

103137
private static class Node<T> {

0 commit comments

Comments
 (0)