# 翻转链表

image-20200520112216561

//定义个链表节点类型
function LinkNode(val){
    this.val = val
    this.next = null
}
//翻转链表
function reverseLink(linkList){
    let curNode = linkList
    let curParent = linklist.next
    while(curNode.next){
        let temp = curParent.next
        curParent.next = curNode
        curNode = curParent
        curParent = curNode
    }
    return curParent
}

//测试方法
//根据数组生成链表
function generateLinks(arr){
    let root = cur = new LinkNode(0)
    arr.forEach(el=>{
        cur.next = new LinkNode(el)
        cur = cur.next
    })
    return root.next
}
let link1 = generateLinks([1,2,3,4,5])
console.log(reverseLink(link1))



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

参考答案:

image-20200520172925623

更新时间: 5/5/2023, 11:19:52 AM