'How to convert circular linked list into singly linked list
First, I make circular linked list that look like
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> (head)
Now, I am trying to convert this circular linked list into singly linked list that's my expected output. I'm trying to remove circularity by adding null
in place of (head)
.
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Here down is details of methods which is declared in my code:
- add(arg) : Adding
Node
from last in circular linked list. - detectCycle() : Detect, linked list is circular or singly.
- detectFirstNode() : If linked list is cyclic, Detect first node where cycle is created.
- printList() : Print the circular linked list.
- printList2() : Print the singly linked list after convert circular to singly linked list.
Here down is my code:
public class Main
{
class Node
{
Integer data;
Node next;
Node(Integer data)
{
this.data = data;
this.next = null;
}
}
Node head = null;
Node tail = null;
public void add(Integer data)
{
Node newNode = new Node(data);
if(head == null)
{
head = newNode;
tail = newNode;
}
else
{
tail.next = newNode;
}
tail = newNode;
tail.next = head;
}
public Node detectCycle()
{
if(head == null)
{
System.out.print("list is empty");
}
Node slow = head;
Node fast = head;
while(fast != null && fast.next != null)
{
fast = fast.next.next;
slow = slow.next;
if(fast == slow)
{
return slow;
}
}
return null;
}
public Integer detectFirstNode()
{
Node meetingNode = detectCycle();
Node start = head;
// Here i want to remove connection of that node with null who create circular linked list
while(start != meetingNode)
{
meetingNode = meetingNode.next;
start = start.next;
}
return start.data;
}
public void printList()
{
Node curr = head;
do
{
System.out.print(curr.data + " -> ");
curr = curr.next;
}while(curr != head);
}
public void printList2()
{
Node currNode = head;
while(currNode != null)
{
System.out.print(currNode.data + " -> ");
currNode = currNode.next;
}
System.out.println("null");
}
public static void main(String[] args) {
Main m = new Main();
m.add(1);
m.add(2);
m.add(3);
m.add(4);
m.add(5);
m.add(6);
m.printList();
System.out.println("\nFirst Node who create circular linked list is : " + m.detectFirstNode());
m.printList2();
}
}
Solution 1:[1]
In your Detect first node method change it to do-while and when you encounter the first node as the meeting nodes next, exit and make it null.
public Integer detectFirstNode() {
Node meetingNode = detectCycle();
Node start = head;
do{
meetingNode = meetingNode.next;
} while (start != meetingNode.next);
meetingNode.next = null;
return start.data;
}
Solution 2:[2]
If I understand your question properly, I will suggest to modify printList2()
method something like below:
public void printList2() {
Node currNode = head;
while (currNode != null) {
System.out.print(currNode.data + " -> ");
if (currNode.next.data == this.detectFirstNode())
currNode = null;
else
currNode = currNode.next;
}
System.out.println("null");
}
Solution 3:[3]
I saw that you were using 2 pointers:
head
: points to first node of the circular linked listtail
: points to last node of the circular linked list,tail -> next
points tohead
Your question is how to convert circular linked list to singly linked list. Obviously, you only point tail -> next
to null
instead of head
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Faeem azaz Bhanej |
Solution 2 | Ashish Patil |
Solution 3 | siroWall |