0

I implemented a singly linked list in Java. I can reverse a singly linked list and check whether a given list is circular. The interesting thing is I can reverse a circular list, too which is strange and interesting. Does it make sense to be able to reverse a circular list?Actually it should get reversed over and over again right? At the moment my below code is able to reverse a circular list and terminates. Is it correct?

public class ListNode{
 int value;
 ListNode next; 
 public ListNode(int value, ListNode next){
 this.value = value;
 this.next = next;
 } 
 public ListNode next(){
 return next;
 }
 public void setNext(ListNode next){
 this.next = next;
 }
 public int value(){
 return value;
 }
}
public class SinglyLinkedList {
 private ListNode head;
 public SinglyLinkedList(ListNode head){
 this.head = head;
 }
 public void reverse(){
 ListNode current = head;
 head = null;
 while (current!=null){
 ListNode temp = current;
 current = current.next;
 temp.next = head;
 head = temp;
 }
 }
 public static boolean isCircular(SinglyLinkedList list){
 ListNode counter1 = list.head;
 ListNode counter2 = list.head;
 while (counter1!=null && counter2!=null){
 counter1 = counter1.next;
 counter2 = counter2.next;
 if (counter2.next!=null){
 counter2 = counter2.next;
 } else 
 return false;
 if (counter1 == counter2)
 return true;
 }
 return false;
 }
 public static void printSinglyLinkedList(ListNode head){
 ListNode temp = head;
 while(temp!=null){
 System.out.print(temp.value + " ");
 temp = temp.next;
 }
 System.out.println();
 }
 public static void main(String[] s){
 ListNode a4 = new ListNode(4, null);
 ListNode a3 = new ListNode(3, a4);
 ListNode a2 = new ListNode(2, a3);
 ListNode a1 = new ListNode(1, a2);
 a4.setNext(a1);
 SinglyLinkedList list1 = new SinglyLinkedList(a1);
 System.out.println(isCircular(list1));
 if (!isCircular(list1))
 printSinglyLinkedList(list1.head);
 list1.reverse();
 if (!isCircular(list1))
 printSinglyLinkedList(list1.head);
 }
}
Bernhard Barker
55.7k14 gold badges110 silver badges143 bronze badges
asked Oct 1, 2012 at 0:01
9
  • 2
    Given your implementation of reverse, it seems strange that a circular list is reversible, as your stopping condition, that current is eventually null, should never hit in a circular list. Commented Oct 1, 2012 at 0:05
  • @MarkElliot As you can see, list1 is circular but at the same time reversible. My very first question is whether it makes sense to be able to reverse a circular singly linked list? From understanding point of view in my opinion, it makes sense to have two different directions of how I go through a list. Is my thinking correct? Commented Oct 1, 2012 at 0:09
  • I'm having problems with this too. Being able to reverse a circular list is far from strange, in fact direction of search in terms of finding or not finding, is irrelevant, that's one of the properties that identifies a circular list. Commented Oct 1, 2012 at 0:16
  • @Bob it only makes sense if the items are ordered. Commented Oct 1, 2012 at 0:18
  • @Bob, I'd be interested in seeing some output of a reversed list with loop, I suppose your head-node would be unchanged, and any path before the loop begins would also end up unchanged, but the loop itself would flip, now that I think through it... Commented Oct 1, 2012 at 0:27

1 Answer 1

0

a4.setNext(a1);

Remove the above statement. This makes it circular.

answered Feb 28, 2013 at 10:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.