-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
61 lines (58 loc) · 1.26 KB
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Created by Inno Fang on 2017/12/16.
*/
/**
* Definition for singly-linked list.
* class ListNode(var `val`: Int = 0) {
* var next: ListNode? = null
* }
*/
class Solution {
fun reverseList(head: ListNode?): ListNode? {
var p = head
var q = head?.next
var h = head
h?.next = null
while (q != null) {
p = q
q = q.next
p.next = h
h = p
}
return h
}
}
class Solution2 {
fun reverseList(head: ListNode?): ListNode? {
if (head == null) return head
var curr = head
var last: ListNode? = null
var next: ListNode? = null
while (curr != null) {
next = curr.next
curr.next = last
last = curr
curr = next
}
return last
}
}
class ListNode(var `val`: Int = 0) {
var next: ListNode? = null
}
fun main(args: Array<String>) {
val head = ListNode(4)
val node1 = ListNode(3)
val node2 = ListNode(2)
val node3 = ListNode(1)
head.next = node1
node1.next = node2
node2.next = node3
node3.next = null
var h = Solution2().reverseList(head)
while (h != null) {
print("${h.`val`} ")
h = h.next
}
println()
}