๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[Coding Interview University] ๋งํฌ๋ ๋ฆฌ์คํธ : ์ด๋ก ๋ฐ ๊ตฌํ ๋ณธ๋ฌธ
[Coding Interview University] ๋งํฌ๋ ๋ฆฌ์คํธ : ์ด๋ก ๋ฐ ๊ตฌํ
์ง์ง์ํ์นด 2023. 6. 29. 21:13<๋ณธ ๋ธ๋ก๊ทธ๋ ์ฝ๋ฉ์ธํฐ๋ทฐ๋ํ ๋์ Git๊ณผ ์์ ์ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
๐ซง ์ฐ๊ฒฐ ๋ฆฌ์คํธ
: ์ฐจ๋ก๋ก ์ฐ๊ฒฐ๋ ๋ ธ๋๋ฅผ ํํํด์ฃผ๋ ์๋ฃ๊ตฌ์กฐ
: ๋จ) ๋ฐฐ์ด๊ณผ ๋ฌ๋ฆฌ ํน์ ์ธ๋ฑ์ค๋ฅผ ์์ ์๊ฐ์ ์ ๊ทผํ ์ ์๋ค
(k๋ฒ์งธ ์์๋ฅผ ์ฐพ๊ณ ์ถ๋ค๋ฉด ์ฒ์๋ถํฐ k๋ฒ ๋ฃจํ ๋์์ผํจ)
: ์ฅ) ๋ฆฌ์คํธ์ ์์ ์ง์ ์์ ์์ดํ ์ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ์ฐ์ฐ์ ์์ ์๊ฐ์ ํ ์ ์๋ค
- ๋จ๋ฐฉํฅ
- ๊ฐ ๋ ธ๋๋ ๋ค์ ๋ ธ๋ ๊ฐ๋ฆฌํด
- ์๋ฐฉํฅ
- ๊ฐ ๋ ธ๋๋ ๋ค์ ๋ ธ๋์ ์ด์ ๋ ธ๋๋ฅผ ํจ๊ป ๊ฐ๋ฆฌํด
๐ (๋จ๋ฐฉํฅ) ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ
class Node {
Node next = null;
int data;
public Node(int d) {
data = d;
}
void appendToTail(int d) {
Node end = new Node(d);
Node n = this;
while (n.next != null) {
n = n.next;
{
n.next = end;
}
}
๐ (๋จ๋ฐฉํฅ) ์ฐ๊ฒฐ ๋ฆฌ์คํธ ์ญ์ ํ๊ธฐ
- ๋ ธ๋ n ์ด ์ฃผ์ด์ง๋ฉด, ์ด์ ๋ ธ๋ prev ๋ฅผ ์ฐพ์ prev.next ์ n.next ๊ฐ ๊ฐ๋๋ก ์ค์ ํ๋ค
- ์๋ฐฉํฅ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ผ ๊ฒฝ์ฐ, n.next๊ฐ ๊ฐ๋ฆฌํค๋ ๋ ธ๋๋ฅผ ๊ฐฑ์ ํ์ฌ n.next.prev ๊ฐ n.prev ๊ฐ ๊ฐ๋๋ก ์ค์
๐ฅ ์ฃผ์
- ๋ ํฌ์ธํธ ๊ฒ์ฌ๋ฅผ ๋ฐ๋์ ํ๋ค
- head์ tail ํฌ์ธํฐ๋ฅผ ๊ฐฑ์ ํ๋ค
Node deleteNode(Node head, int d) {
Node n = head;
if (n.data == d) {
return head.next; // head ์์ง์ด๊ธฐ
}
while (n.next != null) {
if (n.next.data == d) {
n.next = n.next.next;
return head; // head ์์ง์ด์ง ์์
}
}
return head;
}
๐ Runner (๋ถ๊ฐ ํฌ์ธํฐ)
: ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ํํ ๋ ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ๋์์ ์ฌ์ฉํ๋ ๊ฒ
: ํ ํฌ์ธํฐ๊ฐ ๋ค๋ฅธ ํฌ์ธํฐ๋ณด๋ค ์์๋๋ก ํ๋ค
- ์์ ํฌ์ธํฐ๋ ๋ค ํฌ์ธํฐ๋ณด๋ค ํญ์ ์ง์ ๋ ๊ฐ์๋งํผ์ ์์ค ์ ์๋ค
- ๋ค ํฌ์ธํฐ๋ฅผ ์ฌ๋ฌ ๋ ธ๋๋ฅผ ํ๋ฒ์ ๋ฐ์ด๋๋๋ก ์ค์ ํ ์ ์๋ค
๐ ์ฌ๊ท ๋ฌธ์
: ์ฌ๊ท ํธ์ถ ๊น์ด๊ฐ n์ผ ๊ฒฝ์ฐ, ํด๋น ์ฌ๊ท ์๊ณ ๋ฆฌ์ฆ์ด ์ ์ด๋ O(n) ์ ๊ณต๊ฐ์ ์ฌ์ฉ
๐ซง ๋งํฌ๋ ๋ฆฌ์คํธ ๊ตฌํํ๊ธฐ
โก๏ธ size() - ๋ฆฌ์คํธ ์์ ๋ฐ์ดํฐ ๊ฐ์๋ฅผ ๋ฐํํ๋ค.
LinkedList<Integer> list = new LinkedList<Integer>(Arrays.asList(1,2,3));
System.out.println(list.size()); //list ํฌ๊ธฐ : 3
โก๏ธ empty() - ๋ฆฌ์คํธ๊ฐ ๋น์ด์๋ค๋ฉด true๋ฅผ ๋ฐํํ๋ค.
// ๋ฆฌ์คํธ๊ฐ ๋น์๋๊ฐ
template <typename E>
bool SLinkedList<E>::empty() const{
return head == NULL;
}
โก๏ธ value_at(index) - index๋ฒ์งธ ์์น์ value์ ๋ฐํํ๋ค. (๊ฐ์ฅ ์์ 0๋ถํฐ ์์ํ๋ค.)
# declaring array
a = [18, 22, 33, nil, 5, 6]
puts "values_at() method form : #{a.values_at(2, 4)}\\n\\n"
// values_at() method form : [33, 5]
โก๏ธ push_front(value) - ๊ฐ์ฅ ์์ value๋ฅผ ์ถ๊ฐํ๋ค.
int main(void) {
list<int> L = { 3, 7 };
L.push_front(1); // { 1, 3, 7 }
return 0;
}
โก๏ธ pop_front() - ๊ฐ์ฅ ์์ ์๋ ๊ฒ์ ์ ๊ฑฐํ๊ณ , ๊ทธ value๋ฅผ ๋ฐํํ๋ค.
int main(void) {
list<int> L = { 1, 5, 3, 7, 10 }
L.pop_front(); // { 5, 3, 7, 10 }
return 0;
}
โก๏ธ push_back(value) - ๊ฐ์ฅ ๋์ value์ ์ถ๊ฐํ๋ค.
int main(void) {
list<int> L = { 3, 7 };
L.push_back(10); // { 3, 7, 10 }
return 0;
}
โก๏ธ pop_back() - ๊ฐ์ฅ ๋์ ์๋ ๊ฒ์ ์ ๊ฑฐํ๊ณ , ๊ทธ value๋ฅผ ๋ฐํํ๋ค.
int main(void) {
list<int> L = { 1, 5, 3, 7, 10 }
L.pop_back(); // { 5, 3, 7, 10}
return 0;
}
โก๏ธ front() - ๊ฐ์ฅ ์์ ์๋ ๊ฒ์ value๋ฅผ ๊ฐ์ ธ์จ๋ค.
class CircleList{
public:
CircleList(); // ์์ฑ์
~CircleList(); // ์๋ฉธ์
const string& front() const; // ์ปค์ ๋ค์์ ์์
};
โก๏ธ back() - ๊ฐ์ฅ ๋์ ์๋ ๊ฒ์ value๋ฅผ ๊ฐ์ ธ์จ๋ค.
class CircleList{
public:
CircleList(); // ์์ฑ์
~CircleList(); // ์๋ฉธ์
const string& back() const; // ์ปค์์ ์์
};
โก๏ธ insert(index, value) - index๋ฒ์งธ ์์น์ value๋ฅผ ์ถ๊ฐํ๋ค. ์ฆ, index๋ฒ์งธ์ ์๋ก ์ถ๊ฐ๋ ๊ฒ์ด ๊ธฐ์กด์ index๋ฒ์งธ์ ์๋ ๊ฒ์ ๊ฐ๋ฆฌํจ๋ค.
list<int>::iterator iterInsertPos = list1.begin();
list1.insert(iterInsertsPos, 100); // ์ฒซ ๋ฒ์งธ ์์น์ 100 ๋ฃ๊ธฐ
โก๏ธ erase(index) - index๋ฒ์งธ์ ์๋ ๋ ธ๋๋ฅผ ์ญ์ ํ๋ค.
list1.erase(list1.begin()); // list1 ์ฒซ ๋ฒ ์งธ ์์ ์ญ์
โก๏ธ value_n_from_end(n) - ๋ค์์๋ถํฐ n๋ฒ์งธ์ ์๋ ๋ ธ๋์ value๋ฅผ ๋ฐํํ๋ค.
โก๏ธ reverse() - ๋ฆฌ์คํธ๋ฅผ ๋ค์ง๋๋ค.
public class UnitTest {
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(3);
list.addLast(4);
System.out.println(list);
list.reverse();
System.out.println(list);
}
}
// 1 -> 2 -> 3 -> 4
// 4 -> 3 -> 2 -> 1
โก๏ธ remove_value(value) - value์ ๊ฐ์ ๊ฐ์ ๊ฐ์ง๋ ์ฒซ ๋ฒ์งธ ๋ ธ๋๋ฅผ ์ ๊ฑฐํ๋ค.
'๐ฉโ๐ป ์ปดํจํฐ ๊ตฌ์กฐ > Computer Science' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Coding Interview University] ํด์ํ ์ด๋ธ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.05 |
---|---|
[Coding Interview University] ํ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.03 |
[Coding Interview University] ์คํ : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.07.01 |
[Coding Interview University] ๋ฐฐ์ด : ์ด๋ก ๋ฐ ๊ตฌํ (0) | 2023.06.28 |
[Coding Interview University] ๋น ์ค(Big-O)ํ๊ธฐ๋ฒ (0) | 2023.06.28 |