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

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                var removeNthFromEnd = function(head, n) {
	var list = [],
		currentNode = head;

	while(currentNode.next !== null) {
		list.push(currentNode);
		currentNode = currentNode.next;
	}
	list.push(currentNode);

// Now we have to deal with 3 cases about the initial position of the node-to-be-removed.

/* Case-1 >> When the node-to-be-removed is somewhere in between the last and first nodes of the linked-list.

 A) The link of the node before the removed node is redirected to point to the node the removed node is pointing to.

  B) But now the total length of the list has been reduced by one after removal of the n-th node, so the The new n-th node from the end will be (list.length - n + 1 - 1 )-th node from the beginning of the Linked List i.e. (list.length -n )-th node.

  C) And the node previous to the removed node will be ((list.length -n + 1) - 1 - 1)-th node, i.e. (list.length - n - 1)-th node.

  D) So, now, I have to link the node before the removed node to be redirected to point to the node after the removed node, which will be (list.length -n + 1)-th node.

  D) So I do, list[list.length - n - 1].next = list[list.length -n + 1]
So, to remove the current element from the list, all we have to do is link previous.next with current.next . This way, the current element will be lost in the computer memory and will be available to be cleaned by the garbage collector.
  */

	if (list.length -n - 1 >= 0 && list.length -n + 1 < list.length) {
		list[list.length - n - 1].next = list[list.length -n + 1];
		return list[0];
	}

/* Case-2 >> If the node-to-be-removed was the first node of the Linked-list. 
That is, after removal the position of the previous-node to the removed-node will be negative.
In this case, if the node-removed was the only node of the linked list then for the head of the list, return an empty array if list.length is <=1. ELSE,
If the length is more than one, then return the index 1 element for the head. */
	if(list.length - n - 1 < 0) {
		return list.length <=1 ? [] : list[1]
	}

/* Case - 3 >> If the node-to-be-removed was the last node of the Linked-list. 
That is, after removal the previous node becomes the last node of the Linked-list, then just point its next-node to null.*/

	if (list.length - n + 1 >= list.length) {
		list[list.length - n - 1 ].next = null;
		return list[0];
	}
	
}
              
            
!
999px

Console