Detect Loop in linked list Python
- Get link
- X
- Other Apps
PROGRAM TO DETECT AND REMOVE LOOP IN A LINKED LIST
class
LinkedList {
static
Node head;
static
class
Node {
int
data;
Node next;
Node(
int
d)
{
data = d;
next =
null
;
}
}
// Function that detects loop in the list
void
detectAndRemoveLoop(Node node)
{
// If list is empty or has only one node
// without loop
if
(node ==
null
|| node.next ==
null
)
return
;
Node slow = node, fast = node;
// Move slow and fast 1 and 2 steps
// ahead respectively.
slow = slow.next;
fast = fast.next.next;
// Search for loop using slow and fast pointers
while
(fast !=
null
&& fast.next !=
null
) {
if
(slow == fast)
break
;
slow = slow.next;
fast = fast.next.next;
}
/* If loop exists */
if
(slow == fast) {
slow = node;
while
(slow.next != fast.next) {
slow = slow.next;
fast = fast.next;
}
/* since fast->next is the looping point */
fast.next =
null
;
/* remove loop */
}
}
// Function to print the linked list
void
printList(Node node)
{
while
(node !=
null
) {
System.out.print(node.data +
" "
);
node = node.next;
}
}
// Driver code
public
static
void
main(String[] args)
{
LinkedList list =
new
LinkedList();
list.head =
new
Node(
50
);
list.head.next =
new
Node(
20
);
list.head.next.next =
new
Node(
15
);
list.head.next.next.next =
new
Node(
4
);
list.head.next.next.next.next =
new
Node(
10
);
// Creating a loop for testing
head.next.next.next.next.next = head.next.next;
list.detectAndRemoveLoop(head);
System.out.println(
"Linked List after removing loop : "
);
list.printList(head);
}
}
class
Node:
# Constructor to initialize the node object
def
__init__(
self
, data):
self
.data
=
data
self
.
next
=
None
class
LinkedList:
# Function to initialize head
def
__init__(
self
):
self
.head
=
None
# Function to insert a new node at the beginning
def
push(
self
, new_data):
new_node
=
Node(new_data)
new_node.
next
=
self
.head
self
.head
=
new_node
def
detectAndRemoveLoop(
self
):
if
self
.head
is
None
:
return
if
self
.head.
next
is
None
:
return
slow
=
self
.head
fast
=
self
.head
# Move slow and fast 1 and 2 steps respectively
slow
=
slow.
next
fast
=
fast.
next
.
next
# Search for loop using slow and fast pointers
while
(fast
is
not
None
):
if
fast.
next
is
None
:
break
if
slow
=
=
fast:
break
slow
=
slow.
next
fast
=
fast.
next
.
next
# if loop exists
if
slow
=
=
fast:
slow
=
self
.head
while
(slow.
next
!
=
fast.
next
):
slow
=
slow.
next
fast
=
fast.
next
# Sinc fast.next is the looping point
fast.
next
=
None
# Remove loop
# Utility function to print the linked LinkedList
def
printList(
self
):
temp
=
self
.head
while
(temp):
print
temp.data,
temp
=
temp.
next
# Driver program
llist
=
LinkedList()
llist.head
=
Node(
50
)
llist.head.
next
=
Node(
20
)
llist.head.
next
.
next
=
Node(
15
)
llist.head.
next
.
next
.
next
=
Node(
4
)
llist.head.
next
.
next
.
next
.
next
=
Node(
10
)
# Create a loop for testing
llist.head.
next
.
next
.
next
.
next
.
next
=
llist.head.
next
.
next
llist.detectAndRemoveLoop()
print
"Linked List after removing loop"
llist.printList()
OUTPUT
Linked List after removing loop 50 20 15 4 10
- Get link
- X
- Other Apps
Comments
Post a Comment