每次我都不想接触链表和树的操作
链表结构 /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */
这次要求逆转链表结构(javascript)
核心思想是通过先存储链表当前节点的next数据 --------- let tt = tem.next;
使当前的节点的next指向我们设置的新链表(开始为null)---------------tem.next = newhead;
更新新链表 ---------------------newhead = tem;
更新旧链表准备下一次循环 ----------------------tem = tt;
1 /** 2 * @param {ListNode} head 3 * @return {ListNode} 4 */ 5 var reverseList = function(head) { 6 if(head === null){ 7 return null; 8 } 9 let newhead = null; 10 let tem = head; 11 while (tem != null){ 12 let tt = tem.next; 13 tem.next = newhead; 14 newhead = tem; 15 tem = tt; 16 } 17 return newhead; 18 };