반응형

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Question 2.1 | |
/// <summary> | |
/// 정렬되지 않은 연결 리스트에서 중복 문자를 제거하라. | |
/// (임시 버퍼가 허용되지 않는 상황) | |
/// </summary> | |
/// <typeparam name="T"><code>IComparable</code></typeparam> | |
/// <param name="head">리스트의 시작지점</param> | |
public static void Q1_RemoveDuplicates<T>(LinkedListNode<T> head) where T : IComparable<T> | |
{ | |
if (head == null) return; | |
LinkedListNode<T> current = head; | |
while (current != null) | |
{ | |
LinkedListNode<T> runner = current; | |
while (runner.Next != null) | |
{ | |
if (current.Data.CompareTo(runner.Next.Data) == 0) | |
{ | |
runner.Next = runner.Next.Next; | |
if (runner.Next != null) | |
{ | |
runner.Next.Prev = runner; | |
} | |
} | |
else | |
{ | |
runner = runner.Next; | |
} | |
} | |
current = current.Next; | |
} | |
} | |
#endregion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void Q2_1() | |
{ | |
Random rand = new Random(); | |
LinkedListNode<char> head = new LinkedListNode<char>(Convert.ToChar(rand.Next(65, 91)), null, null); | |
LinkedListNode<char> prev = head; | |
for (int index = 0; index < 10; index++) | |
{ | |
var next = new LinkedListNode<char>(Convert.ToChar(rand.Next(65, 91)), null, prev); | |
prev.SetNext(next); | |
prev = next; | |
} | |
LinkedList.Q1_RemoveDuplicates<char>(head); | |
var a = head; | |
while (a.Next != null) | |
{ | |
var b = a; | |
while (b.Next != null) | |
{ | |
b = b.Next; | |
Assert.AreNotEqual(a.Data, b.Data); | |
} | |
a = a.Next; | |
} | |
} |
반응형
'Develop > 코딩인터뷰' 카테고리의 다른 글
[코딩인터뷰] 문제 2.3 (0) | 2020.04.03 |
---|---|
[코딩인터뷰] 문제 2.2 (0) | 2020.04.03 |
[코딩인터뷰] 문제 1.8 (0) | 2020.04.03 |
[코딩인터뷰] 문제 1.7 (0) | 2020.04.03 |
[코딩인터뷰] 문제 1.6 (0) | 2020.04.03 |
꾸준히 노력하는 개발자 "김예건" 입니다.