MyLinkedList ReverseLinkedList(MyLinkedList myLinkedList) { MyLinkedListNode left = myLinkedList.getFirst(); MyLinkedListNode right = myLinkedList.getLast(); MyLinkedListNode tempNode = null; //Shuffle the right neighbours of left node to point to right. left.getNext().setPrevious(right); //Shuffle the left of neighbours of right node to point to left. right.getPrevious().setNext(left); // make sure that the old left neighbor of right node is now the recent left neighbour of left node // and the old left neighbour of left node is now the recent left neighbor of right node tempNode = left.getPrevious(); left.setPrevious(right.getPrevious()); right.setPrevious(tempNode); // make sure that the old right neighbor of right node is now the recent right neighbour of left node // and the old right neighbour of left node is now the recent right neighbor of right node tempNode = left.getNext(); left.setNext(right.getNext()); right.setNext(tempNode); //Arrange the first and last node as they are at the edge myLinkedList.setFirstNode(right); myLinkedList.setLastNode(left); //Get the first and last after reversing the left and right edge nodes. left = myLinkedList.getFirst(); right = myLinkedList.getLast(); //This is for the remaining inner nodes. for (int i = 1; i < myLinkedList.getCount() / 2; i++) { left = left.getNext(); if (left == right) //left and right have joined for even length of linked list. { break; } right = right.getPrevious(); if (left == right)//left and right have joined for odd length of linked list. { break; } //Shuffle the refences of neighbours of left to point to right. if (left.getNext() == right) // left and right about to join { left.getPrevious().setNext(right); right.getNext().setPrevious(left); left.setNext(right.getNext()); right.setPrevious(left.getPrevious()); left.setPrevious(right); right.setNext(left); break; } left.getNext().setPrevious(right); left.getPrevious().setNext(right); //Shuffle the refences of neighbours of right to point to left. right.getNext().setPrevious(left); right.getPrevious().setNext(left); // make sure that the old left neighbor of right node is now the recent left neighbour of left node // and the old left neighbour of left node is now the recent left neighbor of right node tempNode = left.getPrevious(); left.setPrevious(right.getPrevious()); right.setPrevious(tempNode); // make sure that the old right neighbor of right node is now the recent right neighbour of left node // and the old right neighbour of left node is now the recent right neighbor of right node tempNode = left.getNext(); left.setNext(right.getNext()); right.setNext(tempNode); tempNode = left; left = right; right = tempNode; } return(myLinkedList); }