Thinking--从尾到头打印链表

Thinking系列,旨在利用10分钟的时间传达一种可落地的编程思想。

使用 typescript 生成链表

interface node<T> {
    next: node<T> | null
    element: T
}

class LinkedNode<T> implements node<T> {
    element: T
    next: node<T> | null = null
    constructor(element: T, next: node<T> | null = null) {
        this.element = element
        this.next = next
    }
}

class LinkedList<T> {
    length: number = 0
    head: node<T> | null = null
    append(element: node<T>) {
        let node: node<T> = element
        if (this.head === null) {
            this.head = node
        } else {
          	// 不改变head指针
            let current = this.head
            while (current.next) {
                current = current.next
            }
            current.next = node
        }
        this.length++
    }
}

需求: 如,链表信息{"length":3,"head":{"next":{"next":{"next":null,"element":"c"},"element":"b"},"element":"a"}},链表倒序输出c b a

var testLinkedList = new LinkedList<string>()
testLinkedList.append(new LinkedNode<string>('a'))
testLinkedList.append(new LinkedNode<string>('b'))
testLinkedList.append(new LinkedNode<string>('c'))

常规思路

循环保存,然后倒序输出

function printLinkedListReversing (head: node<string>): void {
    let result: string[] = []
    while(head !== null) {
        result.push(head.element)
        head = head.next
    }
    console.log(result.reverse())
}    

延伸

倒叙输出(先进后出 FILO),典型的栈。
依靠递归(递归本质上是一个栈结构)

function printLinkedListReversing_recursion(head: node<string>): void{
    if (head !== null) {
        if (head.next != null) {
            printLinkedListReversing_recursion(head.next)
        }
        console.log(head.element)
    }
}

需要注意的是,当链表过长时,会导致函数调用的层级过深,可能导致调用栈溢出。

扩展: 数组存储中间状态

function printLinkedListReversing(head: node<string>, result: Array<string> = []): Array<string> {
    if (head !== null) {
        if (head.next != null) {
            printLinkedListReversing(head.next, result)
        }
      	console.log(head.element)
        result.push(head.element)
    }
    return result
}

console.log(printLinkedListReversing(testLinkedList.head, []))	// [ 'c', 'b', 'a' ]

物理结构:

  • 顺序存储结构:数组
  • 链式存储结构:链表

逻辑结构:

  • 线性结构:顺序表、栈、队列
  • 非线性结构:树、图
    在这里插入图片描述
    数组:读操作多、写操作少的场景。 非常高效的随机访问能力;插入和删除元素效率不高。
    链表:灵活地进行插入和删除操作。
查找 更新 插入 删除
数组 O(1) O(1) O(n) O(n)
链表 O(n) O(1) O(1) O(1)
发布了256 篇原创文章 · 获赞 868 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/ligang2585116/article/details/103174577