【题目描述】输入一个链表,反转链表后,输出新链表的表头。
function ListNode(x){
this.val=x;
this.next=null;
}
function reverseList(head){
let pre=null,
temp=null;
while(head){
temp=head.next;
head.next=pre;
pre=head;
head=temp;
}
return pre;
}
【题目描述】输入一个链表,反转链表后,输出新链表的表头。
function ListNode(x){
this.val=x;
this.next=null;
}
function reverseList(head){
let pre=null,
temp=null;
while(head){
temp=head.next;
head.next=pre;
pre=head;
head=temp;
}
return pre;
}