Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>Linked Lists</h2>
<i>(Hooray for Linked Lists)</i>
</br>
</br>
<div id="out"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                window.onload = () => {
  const print = str => {
    const d = document.getElementById("out")
    const m = document.createTextNode(str)
    const br = document.createElement("br")
    d.appendChild(m)
    d.appendChild(br)
    console.log(str)
  }

  const test = (actual, expected) => {
    const passed = actual.trim() === expected.trim() ? "PASSED" : "FAILED"
    print(actual + "  ::  " + expected + "  ::  " + passed)
  }

  const Node = function(data, next) {
    this.data = data
    this.next = next
  }

  const resHead = head => {
    let str = ""
    if (head == null) { }
    else {
      let current = head
      while (current != null) {
        str = str + " " + current.data
        current = current.next
      }
    }
    return str
  }

  const append = (head, data) => {
    if (head == null) head = new Node(data, null)
    else {
      let current = head
      while (current.next != null) {
        current = current.next
      }
      current.next = new Node(data, null)
    }
    return resHead(head)
  }

  const prepend = (head, data) => {
    if (head == null) head = new Node(data, null)
    else {
      let current = head
      head = new Node(data, current)
    }
    return resHead(head)
  }

  /** Not to Index - Starting At 1 */
  const del = (head, position) => {
    if (head == null) return resHead(head)
    else {
      let current = head
      for (let i = 1; i < position; i++) {
        current = current.next
      }
      current.data = current.next.data
      current.next = current.next.next
    }
    return resHead(head)
  }

  /** - Starting At 1 */
  const insrt = (head, data, position) => {
    if (head == null) head = new Node(data, null)
    else {
      let current = head
      let last = head
      for (let i = 1; i < position; i++) {
        last = current
        current = current.next
      }
      current = new Node(data, current)
      last.next = current
    }
    return resHead(head)
  }

  const checkCycle = head => {
    if (head == null) return "FALSE"
    else {
      let alreadyIn = []
      let current = head
      while (current != null) {
        if (alreadyIn.indexOf(current.data) == -1) alreadyIn.push(current.data);
        else return "TRUE"
        current = current.next;
      }
      return "FALSE"
    }
  }

  //Re-initialize each time
  const init = () => new Node(1, new Node(2, new Node(3, new Node(4, null))))
  const cycle = () => new Node(1, new Node(2, new Node(1, null)))

  test(append(init(), 5), "1 2 3 4 5")
  test(append(cycle(), 5), "1 2 1 5")
  test(prepend(init(), 5), "5 1 2 3 4")
  test(prepend(cycle(), 5), "5 1 2 1")
  test(del(null, 3), "")
  test(del(init(), 3), "1 2 4")
  test(del(init(), 2), "1 3 4")
  test(insrt(init(), 5, 2), "1 5 2 3 4")
  test(insrt(init(), 5, 3), "1 2 5 3 4")
  test(checkCycle(init()), "FALSE")
  test(checkCycle(cycle()), "TRUE")
}
              
            
!
999px

Console