问答题1066/1593 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

/**
 * 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) {

};
难度:
2021-07-19 创建

参考答案:

1/** 2 * Definition for singly-linked list. 3 * function ListNode(val) { 4 * this.val = val; 5 * this.next = null; 6 * } 7 */ 8/** 9 * @param {ListNode} head 10 * @return {ListNode} 11 */ 12var reverseList = function(head) { 13 if (head == null || head.next == null) return head; 14 let last = reverseList(head.next); 15 head.next.next = head; 16 head.next = null; 17 return last; 18};

最近更新时间:2021-07-25

赞赏支持

预览

题库维护不易,您的支持就是我们最大的动力!