티스토리 뷰

반응형

 

 

오늘 Day24의 자바 강의에서는

Hangman 게임 만들기를 진행하였다.

 

필자도 모든 강의를 수강하고

코드 또한 작성하였으나

코드 내용 중

File file = new File("");

의 코드로 파일을 생성하는 코드가 있는데

필자는 새로 프로젝트를 만들어서 진행하지 않고

package 단위로 진행하다보니

file이 만들어지지 않아 에러가 발생하였다.

 

아직 정확한 에러를 찾지 못해

포스팅으로 진행하기에는 무리가 있다고 판단해

오늘은 자바 코드 리뷰 포스팅을 바로 진행한다.

 

추후에 문제의 원인을 찾고 나면

포스팅하도록 노력해보겠다!!

 

그럼 바로 Day 24 자바 코드 리뷰 결과부터 확인해보자.

 

 

 

Day24 결과

 

 


이번 문제는 linkedLIst의 중복을 제거한 삽입에 대한 문제였다.

 

우리는 removeDuplicates() 메서드를 작성하면 되었고

필자는 조금 헷갈려서 인터넷의 도움을 받고 문제를 해결했다.

 

 

 

참고한 링크와 코드를 지금 바로 확인해보자. ❗❗

(참고한 사이트 출처 - 링크 클릭 시 이동)

 

전체 소스코드이다.

import java.io.*;
import java.util.*;

class Node {
    int data;
    Node next;

    Node(int d) {
        data = d;
        next = null;
    }

}

class Solution {

    public static Node removeDuplicates(Node head) {
        //Write your code here

        Node current = head;

        while (current != null && current.next != null) {
            while (current.next != null && current.data == current.next.data) {
                current.next = current.next.next;
            }
            current = current.next;
        }
        return head;
    }

    public static Node insert(Node head, int data) {
        Node p = new Node(data);
        if (head == null)
            head = p;
        else if (head.next == null)
            head.next = p;
        else {
            Node start = head;
            while (start.next != null)
                start = start.next;
            start.next = p;

        }
        return head;
    }

    public static void display(Node head) {
        Node start = head;
        while (start != null) {
            System.out.print(start.data + " ");
            start = start.next;
        }
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Node head = null;
        int T = sc.nextInt();
        while (T-- > 0) {
            int ele = sc.nextInt();
            head = insert(head, ele);
        }
        head = removeDuplicates(head);
        display(head);

    }
}

 

 

 

소스 코드를 이해하는 데

큰 어려움은 없을 것이다.

 


 

그럼 오늘도 열공하자. 😊

반응형