Programming/LeetCode 17

[LeetCode] #88. Merge Sorted Array (repeat : 0)

두 개의 배열을 합치고 , 오름차순 정렬로 반환하는 문제이다. sort 메소드를 사용해서 간단히 해결했다. # Description You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instea..

[LeetCode] #1290 Convert Binary Number in a Linked List to Integer (repeat : 0)

LinkedList의 노드에는 이진수로만 이루어져있으며, 이를 십진수로 바꾸는 문제이다. Description Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number. Return the decimal value of the number in the linked list. The most significant bit is at the head of the linked list. Example 1: Input: head = [1,0,1] O..

[LeetCode] #83 Remove Duplicates from Sorted List (repeat : 0)

중복값이 나오면 포인터를 리스트에서 빼는 문제이다. 요소를 Set 자료구조에 넣은 후,Set자료구조에 포함되면 이전포인터가 현재포인터를 건너띄고 다음포인터를 향하도록 하였다. 자바인데 컴파일러를 C로 했더니 계속 오류가 낫다 -- # Description Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] #Solution cl..

[LeetCode] #86 partition List (repeat : 0 )

두시간이나 걸린 문제 .. x보다 작으면 앞에다가 옮기고, x보다 크면 뒤에다가 옮기는 문제다. 노드의 순서는 그대로 유지한 상태여야한다. dummy 노드 두개를 만들어서 x보다 작으면 dummy1에다 넣고 , x보다 크면 dummy2에다 넣은 후 둘이 합치면 된다. #Description Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Examp..

[LeetCode] #9 Remove Nth Node From End of List (repeat:0)

마지막 노드부터 시작하여 인자값으로 주어진 노드를 빼고 head를 리턴하는 문제이다. # Description Given the head of a linked list, remove the nth node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] #Solution class Solution{ public ListNode removeNthFromEnd(ListNode head,int n..

[LeetCode] #141 Linked List Cycle (repeat:1)

이번 문제는 LinkedList에 사이클(Cycle)이 있는지 판단하는 문제이며, 미국 신입개발자들 인터뷰에 많이 나온 문제라고 한다. 이번 문제도 slow와 fast 포인터를 이용하여 쉽게 풀 수 있었다. # Description Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index..

[LeetCode] #876 Middle of the Linked List (repeat:0)

LinkedList의 중간 노드를 찾는 문제인데 미국 신입개발자들 인터뷰에 많이 나온 문제라고 한다. 플로이드의 토끼와 거북이(Floyd Tortoise and Hare) 알고리즘으로 풀었다. # Description Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. # Example 1 Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. # Example 2 Input: head =..