edit-article
Home
Up
Delete
Article Name:
Article Description:
code-quiz-question - reversing a linked list -
Chapter ID/Name:
Status:
Write
Writing
Written
Add Photo:
Owner ID:
Content:
use HTML
Edit Content
<h1 style="text-align: center;">reverse a linked list </h1> <h2>[WHAT]</h2> <ol> <li>[] reversing a linked list -</li> </ol> <h2>[WHY]</h2> <ol> <li>[] </li> </ol> <h2>[WHERE]</h2> <ol> <li>[] linked list is a list of directions, point a to point b, point b to point c, point c to point d</li> <ol> <li>[] we could reverse the linked list to travel back from point d to point c, point c to point b, ???</li> </ol> <li>[] linked list is a series of numbers, [ 120, 133, 122, 119, 130, 135, ] </li> <ol> <li>[] </li> </ol></ol> <h2>[WHEN]</h2> <ol> <li>[]</li> </ol> <h2>[EXAMPLE]</h2> <p>let traverseList = function(linkedList){</p> <p><span style="display: inline !important; float: none; background-color: #f7f7f9; color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> var current = this.start;</span></p> <p><span style="display: inline !important; float: none; background-color: #f7f7f9; color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> while (current !== null) {</span></p> <p><em style="color: #333333; font-family: Monaco,Menlo,Consolas,&quot; courier new&quot;,monospace; font-size: 11px; font-style: italic; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> // do something which the current node</em><br style="color: #333333; font-family: Monaco,Menlo,Consolas,&quot; courier new&quot;,monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" /><span style="display: inline !important; float: none; background-color: #f7f7f9; color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> </span></p> <p> <span style="display: inline !important; float: none; background-color: #f7f7f9; color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;">current = current.next; </span><br style="color: #333333; font-family: Monaco,Menlo,Consolas,&quot; courier new&quot;,monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" /><span style="display: inline !important; float: none; background-color: #f7f7f9; color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 11px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> }</span></p> <p>}</p> <p> </p> <p> </p> <p> </p> <h2>[HOW-TO]</h2> <ol> <li>[]</li> </ol> <h2>[REFERENCE]</h2> <ol> <li>[] https://leetcode.com/problems/reverse-linked-list/solution/ </li> <li>[] <a style="color: #0066cc; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 10px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: underline; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;" href="https://www.i-programmer.info/programming/javascript/5328-javascript-data-structures-the-linked-list.html?start=1" target="_blank">https://www.i-programmer.info/programming/javascript/5328-javascript-data-structures-the-linked-list.html?start=1</a> </li> </ol> <div> <h1 style="text-align: center;">CREATE-task# # -</h1> <h2>[previously]</h2> <ol> <li><strong>[00:00] NEW task IN </strong></li> <ol> <li>[] # # - <a href="/view/task?id=1839" target="_blank">CREATE-task# # </a>- </li> </ol></ol> <h2>[currently]</h2> <ol> <li><strong>[00:00] NEW task IN </strong></li> <ol> <li>[] # # - <a href="/view/task?id=1839" target="_blank">CREATE-task# # </a>-</li> </ol></ol> <h2>[next]</h2> <ol> <li><strong>[00:00] NEW task IN </strong></li> <ol> <li>[] # # - <a href="/view/task?id=1839" target="_blank">CREATE-task# # </a>-</li> </ol></ol> <h2>[reference]</h2> <ol> <li> [] </li> </ol> <h1> </h1> <h1 style="text-align: center;">reverse a linked list</h1> <h2><code></code>iterative <br /><code></code></h2> <p><code><br /><span style="font-size: medium;">var reverseList = function(head) {</span><br /><span style="font-size: medium;"> let current = head;</span><br /><span style="font-size: medium;"> if (!current) {</span><br /><span style="font-size: medium;"> return current;</span><br /><span style="font-size: medium;"> }</span><br /><span style="font-size: medium;"> let curPre = head.next;</span><br /><span style="font-size: medium;"> current.next = null;</span><br /><span style="font-size: medium;"> while (curPre) {</span><br /><span style="font-size: medium;"> // save the next node</span><br /><span style="font-size: medium;"> let tmp = curPre.next;</span><br /><span style="font-size: medium;"> curPre.next = current;</span><br /><span style="font-size: medium;"> current = curPre;</span><br /><span style="font-size: medium;"> curPre = tmp;</span><br /><span style="font-size: medium;"> }</span><br /><span style="font-size: medium;"> return current;</span><br /><span style="font-size: medium;">};</span></code></p> <h2>recursive</h2> <p> </p> <p><span style="font-size: medium;"><code>var reverseList = function(head){ </code></span></p> <p><span style="font-size: medium;"><code> if (!head) { return head; } </code></span></p> <p><span style="font-size: medium;"><code> let pre = head.next; </code></span></p> <p><span style="font-size: medium;"><code> head.next = null; </code></span></p> <p><span style="font-size: medium;"><code> return fun(head, pre); </code></span></p> <p> </p> <p><span style="font-size: medium;"><code>function fun(cur, pre){ </code></span></p> <p><span style="font-size: medium;"><code> if (pre == null) { return cur; } </code></span></p> <p><span style="font-size: medium;"><code> let tmp = pre.next; </code></span></p> <p><span style="font-size: medium;"><code> pre.next = cur; </code></span></p> <p><span style="font-size: medium;"><code> return fun(pre, tmp); } </code></span></p> <p><span style="font-size: medium;"><code>}</code></span></p> <p> </p> </div>