Implement a Linked List
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
class LinkedListNode(object):
def __init__(self,value):
self.value = value
self.nextnode = None
a = LinkedListNode(1)
b = LinkedListNode(2)
c = LinkedListNode(3)
a.nextnode = b
b.nextnode = c
Doubly Linked List
class DoublyLinkedListNode(object):
def __init__(self,value):
self.value = value
self.next_node = None
self.prev_node = None
a = DoublyLinkedListNode(1)
b = DoublyLinkedListNode(2)
c = DoublyLinkedListNode(3)
# Setting b after a
b.prev_node = a
a.next_node = b
# Setting c after a
b.next_node = c
c.prev_node = b
Good Job!
Implement a Linked List的更多相关文章
- C++开发工程师面试题库 200~250道
199 MFC中SendMessage和PostMessage的区别?答:PostMessage 和SendMessage的区别主要在于是否等待应用程序做出消息处理.PostMessage只是把消息 ...
- CLRS10.2-8练习 - 单指针值实现双向链表
要求: Explain how to implement doubly linked lists using only one pointer value x.np peritem instead o ...
- 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 ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [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 ...
- [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个元 ...
随机推荐
- 前端魔法堂:解秘FOUC
前言 对于问题多多的IE678,FOUC(flash of unstyled content)--浏览器样式闪烁是一个不可忽视的话题,但对于ever green的浏览器就不用理会了吗?下面尝试较全面 ...
- 导出Mysql数据库中的数据
使用mysqldump 指令: D:\>mysqldump -u root -proot bookStore>bookStore.sql
- IE报vuex requires a Promise polyfill in this browser问题解决
使用Vuex, IE浏览器报错 因为使用了 ES6 中用来传递异步消息的的Promise,而IE低版本的浏览器不支持. ##解决方法 第一步: 安装 babel-polyfill . babel-po ...
- eNSP自学入门(基础)
写了上篇博客之后,就立即投入到了eNSP的怀抱之中了,自己从零基础,入门到现在.也学了不少东西,在这里和大家分享一下. 说一下学习的过程吧,老师说做网络工程的课程设计用eNSP,关于这个软件什么都没有 ...
- 基于python的知乎开源爬虫 zhihu_oauth使用介绍
今天在无意之中发现了一个知乎的开源爬虫,是基于Python的,名字叫zhihu_oauth,看了一下在github上面star数还挺多的,貌似文档也挺详细的,于是就稍微研究了一下.发现果然很好用啊.就 ...
- poj2398计算几何叉积
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...
- 实现javascript下的模块组织
前面的话 java有类文件.Python有import关键词.Ruby有require关键词.C#有using关键词.PHP有include和require.CSS有@import关键词,但是对ES5 ...
- struts的package的name与namespace
<struts> <constant name="struts.devMode" value="true"></constant& ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- 开启MongoDB客户端访问控制
参考官方文档:https://docs.mongodb.org/v2.6/tutorial/enable-authentication/ 基于版本:MongoDB 2.6 概览 在MongoDB数据实 ...