ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` function reverseList(head) { // 如果head 不存在或者head.next不存在,则直接返回 if (!head || !head.next) return head; // 初始设置为空,反转后最后一个节点的next应该为null let pre = null; let next; let cur = head; // 判断当前节点是否为空,不为空则继续进行反转 // 然后把当前节点的next设为上一个节点 // 然后把cur设为下一个节点,pre设置为当前节点 // 相当于把第一个节点变为最后一个节点,然后依次反转 while (cur) { next = cur.next; cur.next = pre; pre = cur; cur = next } } ```