article

reverse a linked list 

[WHAT]

  1. [] reversing a linked list -

[WHY]

  1. [] 

[WHERE]

  1. [] linked list is a list of directions, point a to point b, point b to point c, point c to point d
    1. [] we could reverse the linked list to travel back from point d to point c, point c to point b, ???
  2. [] linked list is a series of numbers, [ 120, 133, 122, 119, 130, 135, ] 
    1. [] 

[WHEN]

  1. []

[EXAMPLE]

let traverseList = function(linkedList){

  var current = this.start;

  while (current !== null) {

     // do something which the current node
  

    current = current.next;
  }

}

 

 

 

[HOW-TO]

  1. []

[REFERENCE]

  1. [] https://leetcode.com/problems/reverse-linked-list/solution/ 
  2. [] https://www.i-programmer.info/programming/javascript/5328-javascript-data-structures-the-linked-list.html?start=1 

CREATE-task# # -

[previously]

  1. [00:00] NEW task IN
    1. [] # # - CREATE-task# #

[currently]

  1. [00:00] NEW task IN
    1. [] # # - CREATE-task# # -

[next]

  1. [00:00] NEW task IN
    1. [] # # - CREATE-task# # -

[reference]

  1.  [] 

 

reverse a linked list

iterative


var reverseList = function(head) {
    let current = head;
    if (!current) {
        return current;
    }
    let curPre = head.next;
    current.next = null;
    while (curPre) {
        // save the next node
        let tmp = curPre.next;
        curPre.next = current;
        current = curPre;
        curPre = tmp;
    }
    return current;
};

recursive

 

var reverseList = function(head){

  if (!head) { return head; }

  let pre = head.next;

  head.next = null;

  return fun(head, pre);

 

function fun(cur, pre){

  if (pre == null) { return cur; }

  let tmp = pre.next;

  pre.next = cur;

  return fun(pre, tmp); }

}

 

Details Photos Edit more

Details

ID: 5766

NAME: code-quiz-question

DESCRIPTION: code-quiz-question - reversing a linked list -

AUTHOR: article.author/s

EDITOR: article.editor/s

PUBLISHER: article.publisher/s

STATUS: Write

PRIORITY: -5

OWNER ID: 75

Content Photos Edit more

photos

page_photo

actions

Email Email-Owner SMS and