본문 바로가기
1 Day 1 Algorithms

[2019.01.02] Linked List

by 곰돌찌 2019. 1. 2.

Problem


Objective 
Today we're working with Linked Lists. Check out the Tutorial tab for learning materials and an instructional video!


Node class is provided for you in the editor. A Node object has an integer data field, , and a Node instance pointer, , pointing to another node (i.e.: the next node in a list).

Node insert function is also declared in your editor. It has two parameters: a pointer, , pointing to the first node of a linked list, and an integer  value that must be added to the end of the list as a new Node object.

Task 
Complete the insert function in your editor so that it creates a new Node (pass  as the Node constructor argument) and inserts it at the tail of the linked list referenced by the  parameter. Once the new node is added, return the reference to the  node.

Note: If the  argument passed to the insert function is null, then the initial list is empty.

Input Format

The insert function has  parameters: a pointer to a Node named , and an integer value, 
The constructor for Node has  parameter: an integer value for the  field.

You do not need to read anything from stdin.

Output Format

Your insert function should return a reference to the  node of the linked list.

Sample Input

The following input is handled for you by the locked code in the editor: 
The first line contains T, the number of test cases. 
The  subsequent lines of test cases each contain an integer to be inserted at the list's tail.

4
2
3
4
1

Sample Output

The locked code in your editor prints the ordered data values for each element in your list as a single line of space-separated integers:

2 3 4 1

Explanation

, so the locked code in the editor will be inserting  nodes. 
The list is initially empty, so  is null; accounting for this, our code returns a new node containing the data value  as the  of our list. We then create and insert nodes , and  at the tail of our list. The resulting list returned by the last call to  is , so the printed output is 2 3 4 1.


How I solved the problem


# Linked List에 대한 개념을 이해해야함.

# recursion을 써서 next가 null일 때까지 찾은 다음에 넣어주는 방식으로 하고 있음..!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.util.*;
 
class Node {
    int data;
    Node next;
    Node(int d) {
        data = d;
        next = null;
    }
}
 
class Solution {
 
    public static  Node insert(Node head,int data) {
        //Complete this method
        Node temp = new Node(data);
        Node current = head;
 
        if (head == null) {
            return temp;
        } else if (current.next == null) {
            current.next = temp;
        } else {
            insert(current.next, data);
        }
 
        return current;
    }
 
    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 N = sc.nextInt();
 
        while(N-- > 0) {
            int ele = sc.nextInt();
            head = insert(head,ele);
        }
        display(head);
        sc.close();
    }
}
cs


[출처 : https://www.hackerrank.com ]

'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.01.04] More Exceptions  (0) 2019.01.04
[2019.01.03] Exceptions - String to Integer  (0) 2019.01.03
[2018.12.28] Scope  (0) 2018.12.28
[2018.12.27] Abstract Classes  (0) 2018.12.27
[2018.12.26] Inheritance  (0) 2018.12.26

댓글