Implement exchangePairs
This commit is contained in:
		
							parent
							
								
									afe2bd6553
								
							
						
					
					
						commit
						e66fd800cf
					
				
					 1 changed files with 20 additions and 2 deletions
				
			
		|  | @ -94,8 +94,26 @@ public class RecursiveDoubleListItemProcessor<T> extends DoubleListItemProcessor | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void exchangePairs(DoubleListItem<T> list) { |     public void exchangePairs(DoubleListItem<T> list) { | ||||||
|         // TODO: Your training |         if (list == null || list.next == null || list.next.next == null) { | ||||||
|         throw new RuntimeException("unimplemented"); |             return; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         var next = list.next; | ||||||
|  |         var nextNext = next.next; | ||||||
|  |         var rest = nextNext.next; | ||||||
|  | 
 | ||||||
|  |         list.next = nextNext; | ||||||
|  |         nextNext.prev = list; | ||||||
|  | 
 | ||||||
|  |         nextNext.next = next; | ||||||
|  |         next.prev = nextNext; | ||||||
|  | 
 | ||||||
|  |         next.next = rest; | ||||||
|  |         if (rest != null) { | ||||||
|  |             rest.prev = next; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         exchangePairs(list.next.next); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|  |  | ||||||
		Reference in a new issue