Discussion in 'C' started by Tabb1us, Jun 22, 2022.

Tags:
1. ### Tabb1usNew Member

Joined:
Jun 20, 2022
Messages:
4
0
Trophy Points:
1
Gender:
Male
First, I create a circular linked list that looks like this:
Code:
`1 -> 2 -> 3 -> 4 -> 5 -> 6 -> (head)`
Now I'm attempting to convert this circular linked list into a singly linked list, which is the desired result. I'm attempting to eliminate circularity by substituting null for (head).
Code:
`1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null`
This is my code:
Code:
```public class Main
{
class Node
{
Integer data;
Node next;
Node(Integer data)
{
this.data = data;
this.next = null;
}
}

Node tail = null;

{
Node newNode = new Node(data);
{
tail = newNode;
}
else
{
tail.next = newNode;
}

tail = newNode;
}

public Node detectCycle()
{
{
System.out.print("list is empty");
}

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();
// 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()
{
do
{
System.out.print(curr.data + " -> ");
curr = curr.next;
}

public void printList2()
{
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.printList();
System.out.println("\nFirst Node who create circular linked list is : " + m.detectFirstNode());
m.printList2();
}
}```
Can someone assist me?

2. ### Tabb1usNew Member

Joined:
Jun 20, 2022
Messages:
4
0
Trophy Points:
1
Gender:
Male
Code:
```public Integer detectFirstNode() {

Node meetingNode = detectCycle();

do{
meetingNode = meetingNode.next;
} while (start != meetingNode.next);

meetingNode.next = null;

return start.data;
}```