LeetCode/Kotlin | Medium

[LeetCode/Kotlin]Medium - 2807. Insert Greatest Common Divisors in Linked List

뿌꾸 빵 2023. 10. 13. 17:54
728x90
반응형
 

Insert Greatest Common Divisors in Linked List - LeetCode

Can you solve this real interview question? Insert Greatest Common Divisors in Linked List - Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to t

leetcode.com

문제

Given the head of a linked list head, in which each node contains an integer value.

Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.

Return the linked list after insertion.

The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.

Example 1:

Input: head = [18,6,10,3]
Output: [18,6,6,2,10,1,3]
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).
- We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes.
- We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes.
- We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes.
There are no more adjacent nodes, so we return the linked list.

Example 2:

Input: head = [7]
Output: [7]
Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes.
There are no pairs of adjacent nodes, so we return the initial linked list.

Constraints:

  • The number of nodes in the list is in the range [1, 5000].
  • 1 <= Node.val <= 1000

풀이

나의 풀이법

최종 소스코드

class Solution {
    fun insertGreatestCommonDivisors(head: ListNode?): ListNode? {
        fun gcd(a: Int, b:Int): Int = if(b != 0) gcd(b, a % b) else a

        var node = head

        while (node.next != null) {
            val g = gcd(node.`val`, node.next.`val`)

            val temp = node.next
            val new = ListNode(g)

            node.next = new
            new.next = temp

            node = temp
        }

        return head
    }
}
728x90
반응형