{"id":117,"date":"2019-01-21T20:38:35","date_gmt":"2019-01-22T01:38:35","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=117"},"modified":"2019-03-17T15:02:21","modified_gmt":"2019-03-17T19:02:21","slug":"reverse-linked-list","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=117","title":{"rendered":"Reverse Linked List"},"content":{"rendered":"\n<p>Reverse a linked list.<br><br>One way to implement this is with recursion. For n nodes, the reverse function is called n times, once from the caller and n-1 times recursively, making the runtime O(n).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func reverse(node: Node?) -> Node? {\n  if (node == nil || node?.next == nil) {\n    return node\n  }\n  \n  let head = reverse(node: node?.next)\n  \n  node?.next?.next = node\n  node?.next = nil\n\n  return head\n}<\/pre>\n\n\n\n<p>Another way to reverse a list is iteratively. After some setup in constant time, the loop runs n-1 times, making the runtime O(n).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func reverse(node: Node?) -> Node? {\n  var prev = node\n  var cur = node?.next\n  var next = node?.next?.next\n  node?.next = nil\n  \n  while cur != nil {\n    if cur?.next == nil {\n      node = cur\n      cur?.next = prev\n      cur = nil\n    }\n    else {\n      cur?.next = prev\n      prev = cur\n      cur = next\n      next = next?.next\n    }\n  }\n\n  return node\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reverse a linked list. One way to implement this is with recursion. For n nodes, the reverse function is called n times, once from the caller and n-1 times recursively, making the runtime O(n). Another way to reverse a list is iteratively. After some setup in constant time, the loop runs n-1 times, making the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/117"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=117"}],"version-history":[{"count":28,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/117\/revisions"}],"predecessor-version":[{"id":688,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/117\/revisions\/688"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}