Skip to content

Instantly share code, notes, and snippets.

@claytonjwong
Created December 22, 2021 15:26
Show Gist options
  • Save claytonjwong/2188d10ff78e2021877dab6497dcbf18 to your computer and use it in GitHub Desktop.
Save claytonjwong/2188d10ff78e2021877dab6497dcbf18 to your computer and use it in GitHub Desktop.
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
class Solution {
fun reverseList(head: ListNode?): ListNode? {
fun go(node: ListNode? = head, last: ListNode? = null): ListNode? {
var next = node?.next
node?.next = last
if (next == null)
return node
return go(next, node)
}
return go()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment