1. 迭代反转
function reverse(head) {
let beg = null;
let mid = head;
let end = head.next;
while(1) {
head.next = beg;
if (end === null) {
break;
}
beg = head;
head = end;
end = end.next;
}
head = mid;
return head;
}
2. 头插法反转
function reverse(head) {
let new_head = null;
let tem = null;
while(head !== null) {
tem = head;
head = head.next;
tem.next = head;
head = tem;
}
return new_head;
}
3. 就地逆置法反转
function reverse(head) {
let start = head;
let end = head.next;
while(head !== null) {
beg.next = end.next;
end.next = head;
head = end;
end = beg.next
}
}
js-单链表反转
最新推荐文章于 2024-05-10 20:01:45 发布