【数据结构与算法-2】链表

本文由浅入深介绍链表,分为初中高级分别对待面试情形
常考知识点反转、交点、环、拆分、深度拷贝、合并

定义链表
class Node:def __init__(self, val):self.val = valself.next = Noneclass
初级
1 链表添加

对一个链表添加元素

def init(n):head = Node(None)for i in range(n):x = Node(i)
2 单链表翻转

翻转单链表,从前往后遍历 206

def reverse(head: Node):new_head = Nonewhile head:tmp = head.nexthead.next = new_headnew_head = headhead = tmpreturn new_head
3 单链表翻转2

反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 92

def reverse(head: Node):new_head = Nonewhile head:head.next = new_head
链表交点

160


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部