/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ classSolution{ funreverseList(head: ListNode?): ListNode? { if (head==null) returnnull if (head.next==null) return head var pre : ListNode? = null var current : ListNode? = head var next : ListNode? = head.next
while (next!=null){ val temp : ListNode? = next.next current?.next = pre pre = current current = next next = temp } current?.next = pre return current } }
/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ classSolution{ funreverseList(head: ListNode?): ListNode? { return point(null,head) }
funpoint(pre:ListNode?,current:ListNode?):ListNode?{ if (current==null) return pre val next = current.next current.next = pre return point(current,next) } }