{"id":1008245,"date":"2024-12-27T11:00:56","date_gmt":"2024-12-27T03:00:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008245.html"},"modified":"2024-12-27T11:00:58","modified_gmt":"2024-12-27T03:00:58","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e7%a9%ba%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1008245.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efa\u7a7a\u94fe\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083635\/65a10623-4d45-4504-8707-1d4a9feba115.webp\" alt=\"python\u5982\u4f55\u521b\u5efa\u7a7a\u94fe\u8868\" \/><\/p>\n<p><p> \u521b\u5efa\u7a7a\u94fe\u8868\u5728Python\u4e2d\u662f\u4e00\u9879\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002<strong>\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u5217\u8868\u7c7b\u578b\u3001\u7c7b\u548c\u8282\u70b9\u6765\u521b\u5efa\u7a7a\u94fe\u8868<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u89e3\u91ca\u5176\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Python\u5185\u7f6e\u5217\u8868<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u5217\u8868\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u521b\u5efa\u94fe\u8868\uff0c\u5c3d\u7ba1\u5b83\u4eec\u672c\u8d28\u4e0a\u662f\u52a8\u6001\u6570\u7ec4\uff0c\u4f46\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f5c\u4e3a\u94fe\u8868\u6765\u4f7f\u7528\u3002\u8981\u521b\u5efa\u4e00\u4e2a\u7a7a\u94fe\u8868\uff0c\u53ea\u9700\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_list = []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u5e76\u4e14Python\u7684\u5217\u8868\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u65b9\u6cd5<\/strong>\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u3001\u79fb\u9664\u5143\u7d20\u6216\u8fdb\u884c\u7d22\u5f15\u8bbf\u95ee\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u5177\u5907\u94fe\u8868\u7684\u6240\u6709\u7279\u6027\uff08\u4f8b\u5982\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\u65f6\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff09\uff0c\u4f46\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u5e94\u7528\u6765\u8bf4\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7c7b\u548c\u8282\u70b9\u5b9e\u73b0\u94fe\u8868<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u771f\u6b63\u7684\u94fe\u8868\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u7c7b\u548c\u94fe\u8868\u7c7b\u6765\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u4e2d\uff0c\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u6570\u636e\u548c\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9a\u4e49\u8282\u70b9\u7c7b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u8282\u70b9\u7c7b\u6765\u8868\u793a\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Node:<\/p>\n<p>    def __init__(self, data=None):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.next = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7c7b\u4e2d\uff0c<code>data<\/code>\u7528\u4e8e\u5b58\u50a8\u8282\u70b9\u7684\u6570\u636e\uff0c<code>next<\/code>\u7528\u4e8e\u5b58\u50a8\u5bf9\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u5f15\u7528\u3002\u5f53\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u4f20\u5165\u6570\u636e\u3002\u521d\u59cb\u72b6\u6001\u4e0b\uff0c<code>next<\/code>\u88ab\u8bbe\u7f6e\u4e3a<code>None<\/code>\uff0c\u8868\u793a\u8be5\u8282\u70b9\u6ca1\u6709\u8fde\u63a5\u5230\u4efb\u4f55\u5176\u4ed6\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b9a\u4e49\u94fe\u8868\u7c7b<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49\u4e00\u4e2a\u94fe\u8868\u7c7b\u6765\u7ba1\u7406\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LinkedList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.head = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u94fe\u8868\u7c7b\u4e2d\uff0c<code>head<\/code>\u7528\u4e8e\u5b58\u50a8\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002\u521b\u5efa\u94fe\u8868\u65f6\uff0c<code>head<\/code>\u521d\u59cb\u5316\u4e3a<code>None<\/code>\uff0c\u8868\u793a\u94fe\u8868\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><h3>3. \u5b9e\u73b0\u94fe\u8868\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u94fe\u8868\u7c7b\u6dfb\u52a0\u65b9\u6cd5\u4ee5\u652f\u6301\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u63d2\u5165\u3001\u5220\u9664\u548c\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LinkedList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.head = None<\/p>\n<p>    def insert(self, data):<\/p>\n<p>        new_node = Node(data)<\/p>\n<p>        new_node.next = self.head<\/p>\n<p>        self.head = new_node<\/p>\n<p>    def delete(self, key):<\/p>\n<p>        temp = self.head<\/p>\n<p>        if temp is not None:<\/p>\n<p>            if temp.data == key:<\/p>\n<p>                self.head = temp.next<\/p>\n<p>                temp = None<\/p>\n<p>                return<\/p>\n<p>        while temp is not None:<\/p>\n<p>            if temp.data == key:<\/p>\n<p>                break<\/p>\n<p>            prev = temp<\/p>\n<p>            temp = temp.next<\/p>\n<p>        if temp == None:<\/p>\n<p>            return<\/p>\n<p>        prev.next = temp.next<\/p>\n<p>        temp = None<\/p>\n<p>    def traverse(self):<\/p>\n<p>        current = self.head<\/p>\n<p>        while current:<\/p>\n<p>            print(current.data)<\/p>\n<p>            current = current.next<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>insert<\/code>\u65b9\u6cd5\u5728\u94fe\u8868\u7684\u5934\u90e8\u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9\u3002<\/li>\n<li><code>delete<\/code>\u65b9\u6cd5\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u8282\u70b9\u3002<\/li>\n<li><code>traverse<\/code>\u65b9\u6cd5\u7528\u4e8e\u904d\u5386\u94fe\u8868\u5e76\u6253\u5370\u6bcf\u4e2a\u8282\u70b9\u7684\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4f7f\u7528\u7c7b\u548c\u8282\u70b9\u7684\u65b9\u5f0f\u66f4\u52a0\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u81ea\u884c\u63a7\u5236\u5185\u5b58\u548c\u8282\u70b9\u7ba1\u7406\u7684\u573a\u666f<\/strong>\u3002\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u66f4\u63a5\u8fd1\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u94fe\u8868\u7684\u5b9a\u4e49\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u591a\u5bf9\u94fe\u8868\u7ed3\u6784\u7684\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u521b\u5efa\u7a7a\u94fe\u8868\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u60a8\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f<\/strong>\u3002\u4f7f\u7528Python\u5185\u7f6e\u5217\u8868\u9002\u5408\u7b80\u5355\u548c\u5feb\u901f\u7684\u5b9e\u73b0\uff0c\u800c\u4f7f\u7528\u7c7b\u548c\u8282\u70b9\u5219\u9002\u5408\u9700\u8981\u66f4\u5927\u7075\u6d3b\u6027\u548c\u63a7\u5236\u7684\u60c5\u51b5\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\uff0c\u7406\u89e3\u94fe\u8868\u7684\u57fa\u672c\u6982\u5ff5\u548c\u64cd\u4f5c\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u94fe\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u94fe\u8868\u3002\u901a\u5e38\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u7c7b\u6765\u8868\u793a\u94fe\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4ee5\u53ca\u4e00\u4e2a\u94fe\u8868\u7c7b\u6765\u7ba1\u7406\u8fd9\u4e9b\u8282\u70b9\u3002\u8282\u70b9\u7c7b\u901a\u5e38\u5305\u542b\u6570\u636e\u548c\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002\u94fe\u8868\u7c7b\u53ef\u4ee5\u5305\u542b\u4e00\u4e2a\u5934\u6307\u9488\uff0c\u521d\u59cb\u65f6\u6307\u5411None\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4e2d\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u662f\u5426\u80fd\u66ff\u4ee3\u94fe\u8868\uff1f<\/strong><br \/>Python\u7684\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u548c\u5b57\u5178\u63d0\u4f9b\u4e86\u5f88\u591a\u4fbf\u6377\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u66ff\u4ee3\u94fe\u8868\u3002\u4f8b\u5982\uff0c\u5217\u8868\u652f\u6301\u52a8\u6001\u5927\u5c0f\u548c\u5feb\u901f\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u64cd\u4f5c\uff08\u5982\u5728\u4e2d\u95f4\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\uff09\u4e0a\uff0c\u94fe\u8868\u53ef\u80fd\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u9700\u8981\u79fb\u52a8\u5176\u4ed6\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u7a7a\u94fe\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u5728\u7a7a\u94fe\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8282\u70b9\uff0c\u7136\u540e\u5c06\u5176\u8bbe\u7f6e\u4e3a\u94fe\u8868\u7684\u5934\u8282\u70b9\u3002\u5982\u679c\u94fe\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5219\u9700\u8981\u904d\u5386\u5230\u94fe\u8868\u7684\u672b\u5c3e\uff0c\u5e76\u5c06\u65b0\u8282\u70b9\u6dfb\u52a0\u5230\u6700\u540e\u3002\u5177\u4f53\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u6dfb\u52a0\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u68c0\u67e5\u94fe\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u76f8\u5e94\u5730\u5904\u7406\u3002<\/p>\n<p><strong>Python\u4e2d\u94fe\u8868\u7684\u5e38\u7528\u64cd\u4f5c\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u94fe\u8868\u64cd\u4f5c\u5305\u62ec\u6dfb\u52a0\u8282\u70b9\u3001\u5220\u9664\u8282\u70b9\u3001\u67e5\u627e\u8282\u70b9\u4ee5\u53ca\u904d\u5386\u94fe\u8868\u3002\u4f60\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u65b9\u6cd5\u6765\u904d\u5386\u94fe\u8868\uff0c\u4ece\u5934\u8282\u70b9\u5f00\u59cb\uff0c\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u8282\u70b9\uff0c\u76f4\u5230\u5230\u8fbe\u94fe\u8868\u7684\u672b\u5c3e\u3002\u8fd9\u6837\uff0c\u4f60\u80fd\u591f\u6253\u5370\u94fe\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u521b\u5efa\u7a7a\u94fe\u8868\u5728Python\u4e2d\u662f\u4e00\u9879\u7b80\u5355\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u5217\u8868\u7c7b\u578b\u3001\u7c7b\u548c\u8282\u70b9 [&hellip;]","protected":false},"author":3,"featured_media":1008249,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008245"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1008245"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008245\/revisions"}],"predecessor-version":[{"id":1008250,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1008245\/revisions\/1008250"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1008249"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1008245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1008245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1008245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}