![JAR search and dependency download from the Maven repository](/logo.png)
g2101_2200.s2181_merge_nodes_in_between_zeros.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2101_2200.s2181_merge_nodes_in_between_zeros
// #Medium #Simulation #Linked_List #2023_06_26_Time_980_ms_(25.00%)_Space_69.1_MB_(100.00%)
import com_github_leetcode.ListNode
/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun mergeNodes(head: ListNode): ListNode? {
var temp = head.next
var slow = head
var sum = 0
var fast = temp
while (temp != null) {
if (temp.`val` == 0) {
temp.`val` = sum
sum = 0
slow.next = fast!!.next
slow = temp
fast = fast.next
} else {
sum += temp.`val`
fast = temp
}
temp = temp.next
}
return head.next
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy