https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Linked%20Lists/Linked%20Lists%20Interview%20Problems/Linked%20List%20Interview%20Problems%20-%20SOLUTIONS/Implement%20a%20Linked%20List%20-SOLUTION.ipynb

Implement a Linked List - SOLUTION

Problem Statement

Implement a Linked List by using a Node class object. Show how you would implement a Singly Linked List and a Doubly Linked List!

Solution

Since this is asking the same thing as the implementation lectures, please refer to those video lectures and notes for a full explanation. The code from those lectures is displayed below:

 

Singly Linked List

In [1]:
class LinkedListNode(object):

    def __init__(self,value):

        self.value = value
self.nextnode = None
In [2]:
a = LinkedListNode(1)
b = LinkedListNode(2)
c = LinkedListNode(3)
In [3]:
a.nextnode = b
b.nextnode = c
 

Doubly Linked List

In [4]:
class DoublyLinkedListNode(object):

    def __init__(self,value):

        self.value = value
self.next_node = None
self.prev_node = None
In [5]:
a = DoublyLinkedListNode(1)
b = DoublyLinkedListNode(2)
c = DoublyLinkedListNode(3)
In [6]:
# Setting b after a
b.prev_node = a
a.next_node = b
In [7]:
# Setting c after a
b.next_node = c
c.prev_node = b
 

Good Job!

Implement a Linked List的更多相关文章

  1. C++开发工程师面试题库 200~250道

    199  MFC中SendMessage和PostMessage的区别?答:PostMessage 和SendMessage的区别主要在于是否等待应用程序做出消息处理.PostMessage只是把消息 ...

  2. CLRS10.2-8练习 - 单指针值实现双向链表

    要求: Explain how to implement doubly linked lists using only one pointer value x.np peritem instead o ...

  3. cc150 Chapter 2 | Linked Lists 2.6 Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.

    2.6Given a circular linked list,  implement an algorithm which returns the node at the beginning of ...

  4. [TS] Implement a doubly linked list in TypeScript

    In a doubly linked list each node in the list stores the contents of the node and a pointer or refer ...

  5. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  9. [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...

随机推荐

  1. Google Chrome 默认非安全端口列表

    1, // tcpmux7, // echo 9, // discard 11, // systat 13, // daytime 15, // netstat 17, // qotd 19, // ...

  2. mysql数据库开启日志

    旧版 #开启慢查询 slow_query_log # (超过2秒的SQL语法记录起来,设短一点来记录除错也是一种方法.) long_query_time = 2 log-slow-queries=D: ...

  3. java异常处理机制(try-catch-finally)

    /* * 异常处理机制 * 1.分类:Error和Exception * Error错误是JVM自动报错的,程序员无法解决例如开数组过大int a[]=new int [1024*1024*1024] ...

  4. xmlplus 组件设计系列之二 - 按钮

    除了图标以外,按钮也许是最简单的组件了,现在来看看如何定义按钮组件. 使用原生按钮组件 在 xmlplus 中,HTML 元素也以组件的方式存在.所以,你可以直接通过使用 button 标签或者 in ...

  5. DbVisualizer:Oracle触发器,解决ORA-04098: 触发器 'USER.DECTUSERTEST_TRI' 无效且未通过重新验证

    我没有用orcal的管理工具,而是用的DbVisualizer 9.5.2,管理数据库. 场景:需要在oracle里面实在自增字段,在网上一搜一堆文档,然后自己就找了一段自己写如下: drop tab ...

  6. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...

  7. typedef和define的详细区别

    typedef是一种在计算机编程语言中用来声明自定义数据类型,配合各种原有数据类型来达到简化编程的目的的类型定义关键字. #define是预处理指令.下面让我们一起来看. typedef是C语言语句, ...

  8. Composer 常用命令总结(三)

    init(初始化) 该命令用于创建 composer.json 文件,并进行基础信息配置: $ composer init 可以配置Package name.Description.Author.Mi ...

  9. mui开发app之html5+,5+Runtime,5+sdk,native.js

    说说几个名词 html5:目前最新的html规范,w3c联盟制定,手机端主要由webkit实现规范,对用户来说就是浏览器实现了它 html5+:所谓"+",扩充了html5原本没有 ...

  10. 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开

    [kuangbin带你飞]专题二 搜索进阶 之 A-Eight 这是一道经典的八数码问题.首先,简单介绍一下八数码问题: 八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的 ...