From e66fd800cfa2f08ddd67569de2fcfc25f4bc52dd Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 5 Sep 2021 15:40:09 +0200 Subject: [PATCH] Implement exchangePairs --- .../RecursiveDoubleListItemProcessor.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java b/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java index c4ad6ab..ca359b5 100644 --- a/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java +++ b/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java @@ -94,8 +94,26 @@ public class RecursiveDoubleListItemProcessor extends DoubleListItemProcessor @Override public void exchangePairs(DoubleListItem list) { - // TODO: Your training - throw new RuntimeException("unimplemented"); + if (list == null || list.next == null || list.next.next == null) { + 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