Add method, solution, and reference. (#32135)
* Add method, solution, and reference. * fix: removed stub text
This commit is contained in:
		
				
					committed by
					
						 Parth Parth
						Parth Parth
					
				
			
			
				
	
			
			
			
						parent
						
							1028310819
						
					
				
				
					commit
					d50b4970dc
				
			| @@ -1,10 +1,69 @@ | ||||
| --- | ||||
| title: Reverse a Doubly Linked List | ||||
| --- | ||||
|  | ||||
| ## Reverse a Doubly Linked List | ||||
|  | ||||
| This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/reverse-a-doubly-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>. | ||||
| ### Method: | ||||
| - Reverse the doubly linked list, and update previous and next variables for each member node accordingly. | ||||
| - Define privileged methods add() and reverse(). | ||||
| - add() will find the end of the list and append new entries at this location. | ||||
| - reverse() will swap entries one pair at a time using a temporary variable. | ||||
|  | ||||
| <a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>. | ||||
| ### Solution: | ||||
|  | ||||
| <!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds  --> | ||||
| ```js | ||||
| var Node = function(data, prev) { | ||||
|   this.data = data; | ||||
|   this.prev = prev; | ||||
|   this.next = null; | ||||
| }; | ||||
| var DoublyLinkedList = function() { | ||||
|   this.head = null; | ||||
|   this.tail = null; | ||||
|   // change code below this line | ||||
|   this.add = function(element) { | ||||
|     let node = new Node(element, this.tail); | ||||
|     let currentNode = this.head; | ||||
|     let previousNode; | ||||
|  | ||||
|     if (this.head === null) { | ||||
|       this.head = node; | ||||
|       this.tail = node; | ||||
|     } else { | ||||
| 	    while (currentNode.next) { | ||||
|   	    previousNode = currentNode; | ||||
|         currentNode = currentNode.next; | ||||
|       } | ||||
|       node.prev = currentNode; | ||||
|       currentNode.next = node; | ||||
|       this.tail = node; | ||||
|     } | ||||
|   };  | ||||
|   this.reverse = function() { | ||||
|     let temp = null; | ||||
|     let currentNode = this.head; | ||||
|      | ||||
|     if (this.head === null) { | ||||
|       return null; | ||||
|     } | ||||
|      | ||||
|     this.tail = currentNode; | ||||
|      | ||||
|     while (currentNode) { | ||||
|     	temp = currentNode.prev; | ||||
|       currentNode.prev = currentNode.next; | ||||
|       currentNode.next = temp; | ||||
|       currentNode = currentNode.prev; | ||||
|     } | ||||
|      | ||||
|     if (temp != null) { | ||||
|       this.head = temp.prev; | ||||
|     } | ||||
|   }; | ||||
|   // change code above this line | ||||
| }; | ||||
| ``` | ||||
|  | ||||
| ### Reference: | ||||
| - [Wikipedia](https://en.wikipedia.org/wiki/Doubly_linked_list) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user