Skip to content

Commit 6077f9f

Browse files
committed
Add reverse linked list solution
1 parent b819cdd commit 6077f9f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Example:
3+
* var li = ListNode(5)
4+
* var v = li.`val`
5+
* Definition for singly-linked list.
6+
* class ListNode(var `val`: Int) {
7+
* var next: ListNode? = null
8+
* }
9+
*/
10+
11+
class Solution {
12+
fun reverseList(head: ListNode?): ListNode? {
13+
var prev: ListNode? = null
14+
var current = head
15+
16+
while (current != null) {
17+
val next = current.next
18+
current.next = prev
19+
prev = current
20+
current = next
21+
}
22+
23+
return prev
24+
}
25+
}

0 commit comments

Comments
 (0)