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. Android系统--输入系统(六)模拟输入驱动程序

    Android系统--输入系统(六)模拟输入驱动程序 1. 回顾输入子系统 简单字符设备驱动:应用程序通过调用驱动所实现的函数使能硬件. 输入子系统:由于有多个应用程序使用输入子系统,故肯定使用的是早 ...

  2. java OJ题目判断输入结束(与C语言的EOF结束等价)

    /* * java 作Oj题目是会有输入若干数据的情况,不好判断输入结束符, * 类似于C语言中的EOF符号 * 在这里提供了一种方法 * */ import java.util.Iterator; ...

  3. linux管道(|)与重定向(<>)的异同

    共同点:管道和重定向都改变程序的标准输入或者标准输出 区别: 管道(|)两边都是程序(命令),而重定向(<>)只有左边是程序(命令).即是,管道通过两个子进程来改变两边命令的输入或输出,重 ...

  4. 利用子集构造法实现NFA到DFA的转换

    概述 NFA非有穷自动机,即当前状态识别某个转换条件后到达的后继状态不唯一,这种自动机不便机械实现,而DFA是确定有限状态的自动机,它的状态转换的条件是确定的,且状态数目往往少于NFA,所以DFA能够 ...

  5. Laravel 中使用 Redis 数据库

    一.前言 Redis 是一个开源高效的键值对存储系统,它通常用作为一个数据结构服务器来存储键值对,它可以支持字符串.散列.列表.集合.有序集合. 1. 安装 predis/predis 在 Larav ...

  6. postgres导入其他数据库数据

    最近对postgres数据库进行深入研究,将原来项目中使用的sqlserver数据库中的数据表导入postgres,网上搜索postgres数据导入,除空间数据库可以通过PostGIS 2.0 Sha ...

  7. C#基础知识-编写第一个程序(二)

    通过上一篇数据类型已经介绍了C#中最基本的15种预定义数据类型,了解每一种类型代表的数据以及每种类型的取值范围,这是很重要也是最基本.下面我们通过实例来了解每个类型如何去使用.编写C#程序时我们需要用 ...

  8. 浅谈OA办公软件市场行情

    3.原文:http://www.jiusi.net/detail/472__776__3999__1.html 关键词:oa系统,OA办公软件 浅谈OA办公软件市场行情 中国的OA办公软件市场历经20 ...

  9. 记事本app TOP5(个人观点)

    1.为知笔记 为知笔记定位于高效率工作笔记,主打工作笔记的移动应用,是目前国内唯一一款"工作笔记"的云笔记类产品.除了常用的笔记功能保存的网页.灵感笔记.重要文档.照片.便签等,为 ...

  10. 一步步学习EF Core(2.事务与日志)

    前言 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ 因为在EF Core1.1.2 中我们在EF6.0+中用到的的延迟加载功能并没有被加入,不过在EF ...