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. grunt构建一个项目

    准备工作:grunt基于node环境运行,所有先安装node.js 1.安装grunt,通过node的npm的包管理工具 >npm install grunt --save-dev 2.npm ...

  2. Flask 学习笔记

    Flask 是一个Web应用框架,我也就是一边看书,一边写博文做记录 这本书: 首先安装Flask ,和配置环境,参考这边博客: 然后就开始学习Flask 了. 1.Application and R ...

  3. Linux Shell——流程控制

    1. 创建交互式脚本 使用 echo命令的选项 关于各种命令的使用,可以使用man 命令来查看命令的详细用法介绍.例如,我想看下 echo 的用法和各种选项.可以执行 man echo.执行结果如下: ...

  4. [codevs]1087麦森数

    题目 这个题在noiOJ上是分治专题,这个题包括了很多,求位数,高精度乘,快速幂. 那么单独把这个高精度拿出来做一个自定义函数即可 一.求位数 显而易见,既然是2进制的就是log2X,是10进制就是l ...

  5. pthread的lowlevellock

    pthread的lowlevellock是futex的最简单的锁应用.也是pthread其它同步原语最基本的锁.lowlevellock提供(或实现)了三种锁(方法),一是基于0或1的互斥的锁规则,二 ...

  6. 记录——excel导出lua工具(python实现)

    项目需要一个从excel导出lua配置表的工具,之前的工具是主程写的,效率极差,i7 CPU 一次全部导出要花掉1个多小时.匪夷所思的是,这么渣的效率,居然用了整整一年.当 然,中途有人反映效率差,主 ...

  7. Yii框架后续

    关于Yii框架遗留的知识点. 1.url路由方式 (1).问号传参(默认) eg: http://localhost/项目/app/index.php http://localhost/项目/app/ ...

  8. Python学习之路-Day1-Python基础

    学习python的过程: 在茫茫的编程语言中我选择了python,因为感觉python很强大,能用到很多领域.我自己也学过一些编程语言,比如:C,java,php,html,css等.但是我感觉自己都 ...

  9. 你真的用好了Python的random模块吗?

    random模块 用于生成伪随机数 源码位置: Lib/random.py(看看就好,千万别随便修改) 真正意义上的随机数(或者随机事件)在某次产生过程中是按照实验过程中表现的分布概率随机产生的,其结 ...

  10. SQLite 之 C#版 System.Data.SQLite 使用

    简介 SQLite简介 SQLite,是一款轻型的关系型数据库.它的设计目标是嵌入式. 它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 C++.C ...