題意
將題目給的 ListNode 反轉
題解
將 node 指向的下一個 node 替換成原本的上一個 node
程式碼
/** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun reverseList(head: ListNode?): ListNode? { var head = head var front:ListNode? = null var next: ListNode? = null // var i = 1 while(head != null){ // println("${i++} ${head.`val`} (next : ${head.next.`val`})") //println(i++) next = head!!.next head!!.next = front front = head head = next } return front } }
160ms/36.1 MB (beats 45.63% / 70.37%)
0 comments:
張貼留言