Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var insert = function(value, parent) {
  var node = { val: value };
  if (parent) {
    // Connect a node to the parent.
    parent.next = node;
  }
  
  return node;
}
var traverse = function(root, callback) {
  while (root) {
    if (callback) {
      callback(root);
    }
    
    root = root.next;
  }
}
var reverse = function(node, parent) {
  var result = parent || {};
  
  if (node) {
    var child = node.next;
    node.next = parent;
    result = reverse(child, node);
  }
  return result;
}
// Create a simple linked list, consisting of 5 nodes.
var root = insert(1);
var child = insert(2, root);
child = insert(3, child);
child = insert(4, child);
child = insert(5, child);
// Display the linked list.
traverse(root, function(node) { console.log(node.val); })
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
primaryobjectspro
0viewers