Adding a solution for reversing a DoubleLinkedList. (#38427)
This commit is contained in:
		@@ -122,7 +122,35 @@ DoublyLinkedList.prototype = Object.assign(
 | 
				
			|||||||
<section id='solution'>
 | 
					<section id='solution'>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```js
 | 
					```js
 | 
				
			||||||
// solution required
 | 
					  var Node = function(data, prev) {
 | 
				
			||||||
 | 
					    this.data = data;
 | 
				
			||||||
 | 
					    this.prev = prev;
 | 
				
			||||||
 | 
					    this.next = null;
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  var DoublyLinkedList = function() {
 | 
				
			||||||
 | 
					    this.head = null;
 | 
				
			||||||
 | 
					    this.tail = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    this.reverse = function() {
 | 
				
			||||||
 | 
					      if (!this.head || !this.head.next) {
 | 
				
			||||||
 | 
					        return this.head
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      let tail;
 | 
				
			||||||
 | 
					      let temp;
 | 
				
			||||||
 | 
					      let current = this.head;
 | 
				
			||||||
 | 
					      while(current !== null) {
 | 
				
			||||||
 | 
					        if(!tail) tail = current;
 | 
				
			||||||
 | 
					        temp = current.prev;
 | 
				
			||||||
 | 
					        current.prev = current.next;
 | 
				
			||||||
 | 
					        current.next = temp;
 | 
				
			||||||
 | 
					        current = current.prev;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      this.head = temp.prev;
 | 
				
			||||||
 | 
					      this.tail = tail
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</section>
 | 
					</section>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user