【数据结构与算法-2】链表
生活随笔
收集整理的這篇文章主要介紹了
【数据结构与算法-2】链表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文由淺入深介紹鏈表,分為初中高級(jí)分別對(duì)待面試情形
常考知識(shí)點(diǎn)反轉(zhuǎn)、交點(diǎn)、環(huán)、拆分、深度拷貝、合并
定義鏈表
class Node:def __init__(self, val):self.val = valself.next = Noneclass初級(jí)
1 鏈表添加
對(duì)一個(gè)鏈表添加元素
def init(n):head = Node(None)for i in range(n):x = Node(i)2 單鏈表翻轉(zhuǎn)
翻轉(zhuǎn)單鏈表,從前往后遍歷 206
def reverse(head: Node):new_head = Nonewhile head:tmp = head.nexthead.next = new_headnew_head = headhead = tmpreturn new_head3 單鏈表翻轉(zhuǎn)2
反轉(zhuǎn)從位置 m 到 n 的鏈表。請(qǐng)使用一趟掃描完成反轉(zhuǎn)。 92
def reverse(head: Node):new_head = Nonewhile head:head.next = new_head鏈表交點(diǎn)
160
總結(jié)
以上是生活随笔為你收集整理的【数据结构与算法-2】链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据结构与算法-1】常用数据结构
- 下一篇: 【数据结构与算法】递归