/*** Definition for singly-linked list.* function ListNode(val, next) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }*/
/*** @param {ListNode} head* @return {ListNode}*/1、逐个断键,将后一个节点放到前面
var reverseList = function(head) {var len = getLen(head)var h = headvar pre = headwhile(len>0){var temp = h.nexth.next = nullh.next = temp.nexttemp.next = nulltemp.next = prepre = templen--}return pre
};function getLen(p){var length = 0while(p != null){length ++p=p.next}return length-1
}
2、与1类似但代码较少
var reverseList = function(head) {var pre = nullvar current = headwhile(current != null){var temp = current.nextcurrent.next = prepre = currentcurrent = temp}return pre
};