(力扣)206.反转链表--JavaScript
解题思路
反转两个节点:将n+1的next指向n。
反转多个节点:双指针遍历链表,重复上述操作。
/** * 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} */ var reverseList = function(head) { let p1=head; let p2=null; while(p1){ // console.log(p1.val,p2&&p2.val); const tmp=p1.next; p1.next=p2; p2=p1; p1=tmp; } return p2; };

浙公网安备 33010602011771号