Skip to content

Commit

Permalink
Add scala solution for 206-Reverse-Linked-List
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKheng committed Sep 17, 2022
1 parent b34ea08 commit aa203a7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions scala/206-Reverse-Linked-List.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
object Solution {
def reverseList(head: ListNode): ListNode = {
if (head == null || head.next == null) {
return head
}

val reversedListHead = reverseList(head.next)
head.next.next = head
head.next = null

return reversedListHead
}
}

0 comments on commit aa203a7

Please sign in to comment.