Skip to main content

2023-9-8

9-8

链表

206. 反转链表

第一感觉直接上递归

/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function reverseList(head: ListNode | null): ListNode | null {
if(head===null||head.next===null){
return head
}
let cur = reverseList(head.next)
head.next.next = head
head.next = null
return cur
};