Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

add kotlin solution for 328 Odd-Even-Linked-List.kt #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class OddEvenLinkedListKotlin328 {
fun oddEvenList(head: ListNode?): ListNode? {
if (head?.next?.next == null) {
return head
}
val evenHead = head.next

var odd = head
var even = head.next
var current = even
var count = 0
while (current?.next != null) {
current = current.next
if (count % 2 == 0) {
odd?.next = current
odd = current
} else {
even?.next = current
even = current
}
count++
}
even?.next = null
odd?.next = evenHead
return head
}
/*
fun oddEvenList(head: ListNode?): ListNode? {
if (head?.next?.next == null) {
return head
}
val evenHead = head.next

var odd = head
var even = head.next
var current = even
var isOdd = true
while (current?.next != null) {
current = current.next
if (isOdd) {
isOdd = !isOdd
odd?.next = current
odd = current
} else {
isOdd = !isOdd
even?.next = current
even = current
}
}
even?.next = null
odd?.next = evenHead
return head
}
*/

class ListNode(var `val`: Int) {
var next: ListNode? = null
}
}

fun main() {
val node1 = OddEvenLinkedListKotlin328.ListNode(1)
val node2 = OddEvenLinkedListKotlin328.ListNode(2)
val node3 = OddEvenLinkedListKotlin328.ListNode(3)
val node4 = OddEvenLinkedListKotlin328.ListNode(4)
val node5 = OddEvenLinkedListKotlin328.ListNode(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5

val solution = OddEvenLinkedListKotlin328()
var result = solution.oddEvenList(node1)

while (result?.next != null) {
println(result.`val`)
result = result.next
}
println(result?.`val`)
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.