python实现反向传播

A Linked List is a simple but fascinating data structure that can be used to store linearly connected non-contiguous data.

链表是一种简单但引人入胜的数据结构,可用于存储线性连接的非连续数据。

We are often encountered with interesting manipulative problems using a linked list as they require out-of-the-box thinking with the limited properties of the Singly Linked List.

使用链接列表时,我们经常会遇到有趣的操作问题,因为它们需要开箱即用的思维方式,而且单链接列表的属性有限。

In this article, we will be discussing the problem to Reverse a Singly Linked List.

在本文中,我们将讨论反向链接单链列表的问题

Linked List

链表

Throughout the article, I will be assuming that you are able to comprehend basic terminology related to Linked lists. If that is not the case, please refer to the following article(s) before reading on.

在整篇文章中,我将假设您能够理解与链接列表有关的基本术语。 如果不是这种情况,请在继续阅读之前参考以下文章。

Java LinkedList – LinkedList In Java

Java LinkedList – Java中的LinkedList

演示地址

反向链接列表 (Reversing a Linked List)

Let us dive right into the discussion for the solution. We will discuss two methods:

让我们直接深入讨论该解决方案。 我们将讨论两种方法:

  • Iterative Solution (using 3 pointers)迭代解决方案(使用3个指针)
  • Recursive Solution (using pseudo-2 pointers)递归解决方案(使用伪2指针)

Note: I would suggest you to try to solve the problem, and then go to the solution.

注意 :建议您尝试解决问题,然后再解决。

使用迭代解决方案反向链接列表 (Reverse a Linked List using Iterative Solution)

  • Let us get over with the base cases first. If the linked list has 0 or only 1 node, then it does not make sense to reverse the list, so we can simply return then and there.让我们先克服基本情况。 如果链表只有0个节点或只有1个节点,那么反转链表是没有意义的,因此我们可以简单地从那时到那里返回。
  • Assuming we have >=2 nodes now, we can do the following.假设现在有> = 2个节点,我们可以执行以下操作。
  • Keep 3 pointers on previous node, current node, next node.在上一个节点,当前节点,下一个节点上保留3个指针。
  • Initially, assign the previous node as NULL, current node as the head and next node as the successor of the head.最初,将前一个节点指定为NULL,将当前节点指定为头,然后将下一个节点指定为头的后继。
  • Reverse the link between the previous and current node.反向上一个节点与当前节点之间的链接。
  • Move all the pointers one step forward by doing the following:
    1. Previous node = current node
    2. Current node = next node
    3. Next node = next node -> next

    通过执行以下操作将所有指针向前移一步:

    1. 上一个节点=当前节点
    2. 当前节点=下一个节点
    3. 下一个节点=下一个节点->下一个
  • Go to step 5 until the current node does not become NULL.转到步骤5,直到当前节点不为NULL。
  • Assign head as the previous node and return.将head分配为上一个节点并返回。

The code following this paradigm can be found here.

遵循此范例的代码可在此处找到。

C语言代码 (Code in C)

#include<stdio.h>
#include<stdlib.h>struct node{
int data;
struct node *next;
} *head = NULL;struct node *make_node(int data){
struct node *new = (struct node *)malloc(sizeof(struct node));
new->next = NULL; new->data = data;
return new;
}void push(int data){
struct node *new_node = make_node(data);
new_node->next = head;
head = new_node;
}void print_list(){
struct node *cur = head;
while(cur){
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}void reverse_list(){
if(head == NULL || head->next == NULL)
return;
struct node *prev = NULL, *cur = head, *next;
while(cur){
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
head = prev;
}int main(){push(3);
push(4);
push(5);
push(6);printf("Given Linked list is: ");
print_list();reverse_list();printf("Reversed Linked list is: ");
print_list();return 0;
}

Python代码 (Code in Python)

class Node:def __init__(self, data):self.data = dataself.next = Noneclass LinkedList:def __init__(self):self.head = Nonedef reverse(self):if self.head is None or self.head.next is None:returnprev = Nonecur = self.headwhile cur:next_element = cur.nextcur.next = prevprev = curcur = next_elementself.head = prevdef push(self, data):new_node = Node(data)new_node.next = self.headself.head = new_nodedef print_list(self):cur = self.headl1 = []while cur:l1.append(cur.data)cur = cur.nextreturn l1head = LinkedList()
head.push(3)
head.push(4)
head.push(5)
head.push(6)
print("Given list is: ", head.print_list())
head.reverse()
print("Reversed list is: ", head.print_list())

使用递归解决方案反向链接列表 (Reverse a Linked List using Recursive Solution)

The recursive solution is slightly easy to understand as it uses a more natural and easy-to-understand algorithm. Nevertheless, iterative and recursive solutions are similar in working.

递归解决方案使用更自然且易于理解的算法,因此稍微易于理解。 但是,迭代和递归解决方案在工作上相似。

We mainly use recursion to replace the pointer ‘next’ as we can recurse forward to the end of the linked list and follow in a similar way as the iterative solution.

我们主要使用递归来替换指针“ next”,因为我们可以递归到链表的末尾并以与迭代解决方案类似的方式进行跟踪。

The only differences being that we move backward as well after going to the end of the list, due to the use of recursion.

唯一的区别是,由于使用了递归,我们在移至列表末尾后也向后移动。

Also, note that in the recursive solution we don’t require a next pointer as recursion allows us to move ahead in the linked list.

另外,请注意,在递归解决方案中,我们不需要下一个指针,因为递归允许我们在链接列表中前进。

Here we define the recursive solution in 2 parts:

在这里,我们分为两部分来定义递归解决方案:

  • Recursive Case:

    1. We would first move ahead in the linked list.
    2. When the recursion ends, we can simply link the current node to the previous node.

    递归案例:

    1. 我们将首先在链接列表中前进。
    2. 递归结束后,我们可以简单地将当前节点链接到上一个节点。
  • Base Case: If the current element is NULL, then we can simply assign the head as previous node i.e. the last node of the linked list in this case.基本情况:如果当前元素为NULL,则在这种情况下,我们可以简单地将head分配为链表的前一个节点,即最后一个节点。

The code following this paradigm can be found here:

遵循此范例的代码可以在这里找到:

C语言代码 (Code in C)

#include<stdio.h>
#include<stdlib.h>struct node{
int data;
struct node *next;
} *head = NULL;struct node *make_node(int data){
struct node *new = (struct node *)malloc(sizeof(struct node));
new->next = NULL; new->data = data;
return new;
}void push(int data){
struct node *new_node = make_node(data);
new_node->next = head;
head = new_node;
}void print_list(){
struct node *cur = head;
while(cur){
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}struct node *reverse_list(struct node *prev, struct node *cur){
if(cur == NULL){
head = prev;
}
else{
reverse_list(cur, cur->next);
cur->next = prev;
}
}int main(){push(3);
push(4);
push(5);
push(6);printf("Given Linked list is: ");
print_list();reverse_list(NULL, head);printf("Reversed Linked list is: ");
print_list();return 0;
}

Python代码 (Code in Python)

class Node:def __init__(self, data):self.data = dataself.next = Noneclass LinkedList:def __init__(self):self.head = Nonedef _reverse(self, prev, cur):if cur is None:self.head = prevelse:self._reverse(cur, cur.next)cur.next = prevdef reverse(self):self._reverse(None, self.head)def push(self, data):new_node = Node(data)new_node.next = self.headself.head = new_nodedef print_list(self):cur = self.headl1 = []while cur:l1.append(cur.data)cur = cur.nextreturn l1head = LinkedList()
head.push(3)
head.push(4)
head.push(5)
head.push(6)
print("Given list is: ", head.print_list())
head.reverse()
print("Reversed list is: ", head.print_list())

Output

输出量

Reverse Linked List using Recursion – Python

使用递归的反向链接列表– Python

翻译自: https://www.journaldev.com/30165/reverse-a-linked-list-c-python-implementation

python实现反向传播

python实现反向传播_如何反向链接列表? (C和Python实现)相关推荐

  1. 机器学习 反向传播_机器学习中的神秘化反向传播:您想了解的隐藏数学

    机器学习 反向传播 By Ibrahima "Prof" Traore, ML expert at Wildcard / w6d.io 作者:Wildcard / w6d.io的 ...

  2. 杰弗里·辛顿 反向传播_菲菲·李·杰弗里·塞顿和王座人工智能游戏

    杰弗里·辛顿 反向传播 Winter is coming for artificial intelligence, and it may be years before AI research pic ...

  3. 前向传播和反向传播_深度学习的地基模块:模型、参数、非线性、前向传播、反向偏微分

    头条ID:钱多多先森,关注更多AI.CV.数码.个人理财领域知识,关注我,一起成长 在深度学习中,数据.模型.参数.非线性.前向传播预测.反向偏微分参数更新等等,都是该领域的基础内容.究竟他们最基础的 ...

  4. 反向传播神经网络(BPNN)的实现(Python,附源码及数据集)

    文章目录 一.理论基础 1.前向传播 2.反向传播 3.激活函数 4.神经网络结构 二.BP神经网络的实现 1.训练过程(BPNN.py) 2.测试过程(test.py) 3.测试结果 4.参考源码及 ...

  5. 用python画奥迪标志_不知道不 OK!53 个 Python 经典面试题详解

    作者 | Chris 翻译 | 苏本如,责编 | 夕颜 头图 | CSDN付费下载自视觉中国 出品 | CSDN(ID:CSDNnews) 以下为译文: 本文列出53个Python面试问题,并且提供了 ...

  6. python跑得慢_代码跑得慢甩锅Python?手把手教你如何给代码提速30%

    原标题:代码跑得慢甩锅Python?手把手教你如何给代码提速30% 来源丨Medium 编译丨王转转 大数据文摘出品 https://mp.weixin.qq.com/s/bY3REj6qVw0M1N ...

  7. python 进程生命周期_计算客户生命周期价值的python解决方案

    python 进程生命周期 By Lisa Cohen, Zhining Deng, Shijing Fang, and Ron Sielinski 由丽莎·科恩,志宁邓,石井方和罗恩Sielinsk ...

  8. python中集合运算_入门 | 一文带你了解Python集合与基本的集合运算

    原标题:入门 | 一文带你了解Python集合与基本的集合运算 选自DataCamp 作者:Michael Galarnyk 参与:Geek Ai.思源 一般我们熟悉 Python 中列表.元组及字典 ...

  9. python积木编程软件_童心制物慧编程全新 Python 编辑器正式上线

    原标题:童心制物慧编程全新 Python 编辑器正式上线 作为一款致力于 提供更专业.更多样的STEAM教育解决方案的编程软件,童心制物慧编程一直不断在优化慧编程的教学功能,只为了给所有编程教育者提供 ...

最新文章

  1. 深度学习巨头Yoshua Bengio清华演讲: 深度学习通往人类水平人工智能的挑战
  2. jrtplib 分包处理
  3. av_read_frame 阻塞卡死解决办法
  4. QT5 文件读写操作
  5. ros和java通讯_ROS学习之路(二)——通信架构(上)
  6. IOS开发之Autolayout——“Content Compression Resistance”和“Content Hugging”
  7. 可爱的穆里尼奥,可爱的切尔西!
  8. 老张 .NetCore与Vue 框架学习
  9. 常用animation动画
  10. 在微信公众平台做HTML5游戏经验谈
  11. app软件怎么开发 盘点3种app制作方式
  12. cpu压测 windows_怎么用AIDA64进行CPU压力测试?
  13. python加密狗的制作_制作u盘加密狗
  14. C++课后作业 10.教材习题7_6:哺乳动物类Mammal派生出狗类Dog
  15. 如何用淘宝客为淘宝店铺引流
  16. 【无限互联】学员作品 豆果美食IOS客户端
  17. Mac 升级Python 有python2.7到python3.9
  18. Python爱心表白,快去发给你心仪的人叭~
  19. 新iPad未到 老iPad价格反弹
  20. 数据库查询所有表及实现拼接清表sql

热门文章

  1. QT生成在Windows下有图标的exe文件(IDE=QT Creator)
  2. 【转载】]基于RedHatEnterpriseLinux V7(RHEL7)下SPEC CPU 2006环境搭建以及测试流程 介绍、安装准备、安装、config文件以及运行脚本介绍...
  3. UIImageView contentModel
  4. ubuntu环境下,ubuntu16.04装机到nvdia显卡驱动安装、cuda8安装、cudnn安装
  5. 使用async 和 await方法来
  6. 如何使用数据库引擎优化顾问优化数据库
  7. 【原】c#对xml的操作
  8. html textarea粘贴事件,javascript在textarea中捕获粘贴事件
  9. c java 语法_java程序员要学的c语法
  10. js生成批次号_【管理】MES系统中的批次管理