{"id":2044,"date":"2018-03-08T08:44:14","date_gmt":"2018-03-08T16:44:14","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2044"},"modified":"2018-03-08T08:44:47","modified_gmt":"2018-03-08T16:44:47","slug":"leetcode-206-reverse-linked-list","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-206-reverse-linked-list\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 206. Reverse Linked List"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u53cd\u8f6c\u4e00\u4e2a\u5355\u5411\u94fe\u8868<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/\">https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/<\/a><\/p>\n<p>Reverse a singly linked list.<\/p>\n<p><strong>Solution 1:<\/strong><\/p>\n<p>Tracking prev \/ curr \/ next node<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  ListNode* reverseList(ListNode* head) {\r\n    ListNode* prev = nullptr;\r\n    ListNode* curr = head;\r\n    ListNode* next;\r\n    while (curr) {\r\n      next = curr-&gt;next;\r\n      curr-&gt;next = prev;\r\n      prev = curr;\r\n      curr = next;\r\n    }\r\n    return prev;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\n  public ListNode reverseList(ListNode head) {\r\n    ListNode prev = null;\r\n    ListNode curr = head;\r\n    ListNode next;\r\n    while (curr != null) {\r\n      next = curr.next;\r\n      curr.next = prev;\r\n      prev = curr;\r\n      curr = next;\r\n    }\r\n    return prev;\r\n  }\r\n}<\/pre>\n<p>Python 3<\/p>\n<pre class=\"lang:default decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 48 ms\r\n\"\"\"\r\nclass Solution:\r\n  def reverseList(self, head):\r\n    prev, curr, nxt = None, head, None;    \r\n    while curr:\r\n      nxt, curr.next = curr.next, prev      \r\n      prev, curr = curr, nxt\r\n    return prev<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u53cd\u8f6c\u4e00\u4e2a\u5355\u5411\u94fe\u8868 Problem: https:\/\/leetcode.com\/problems\/reverse-linked-list\/description\/ Reverse a singly linked list. Solution 1: Tracking prev \/ curr \/ next node Time complexity: O(n) Space complexity: O(1) C++ \/\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[249,222,83,247,248,246],"class_list":["post-2044","post","type-post","status-publish","format-standard","hentry","category-list","tag-curr","tag-easy","tag-list","tag-next","tag-prev","tag-reverse","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2044"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2044\/revisions"}],"predecessor-version":[{"id":2046,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2044\/revisions\/2046"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}