This article is part of article series - "Datastructures"
Previous Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
Next Article: Finding Nth node from end of a Singly Linked List.
Once we confirm that there is a Loop in a Singly Linked
List we will see how to determine first node of the loop.
12.25.2012
11.13.2012
Finding Nth node from end of a Singly Linked List
This article is part of article series - "Datastructures"
Previous Article: Finding first node in a Loop in Singly Linked List.
Solution 01 - Brute Force Approach:
Previous Article: Finding first node in a Loop in Singly Linked List.
Figure 1: Singly Linked List |
Solution 01 - Brute Force Approach:
- Start at First Node of the List (call it curNodePtr).
- Assign curNodePtr to tmpPtr and count number of nodes after the curNodePtr.
- If number of nodes after curNodePtr are equal to N nodes or tmpPtr reaches END then break. If tmPtr reaches END but count not equal to N then return since we can't find the Nth node from the end of the Singly Linked List.
- Move the curNodePtr one step forward in the Linked List i.e curNodePtr now points to its next node in the list and start again from STEP-2.
11.06.2012
Reversing a Singly Linked List
This article is part of article series - "Datastructures"
Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
Let us see how to reverse a Singly Linked List.
Pseudocode:
Time Complexity: O(n)
Space Complexity: O(3)
If we try on the example in Figure-1 we get the output as shown below
Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
Let us see how to reverse a Singly Linked List.
Figure-1: Singly Linked List |
cur_ptr = HEAD->NEXT prev_ptr = NULL forever: if cur_ptr == NULL break tmp_ptr = prev_ptr prev_ptr = cur_ptr cur_ptr = cur_ptr->NEXT prev_ptr->NEXT = tmp_ptr HEAD->NEXT = prev_ptrComplexity:
Time Complexity: O(n)
Space Complexity: O(3)
If we try on the example in Figure-1 we get the output as shown below
Figure-2: After reversing the Singly Linked List |
Previous Article: Deleting a Node from a Singly Linked List.
Next Article: Detecting a Loop in Singly Linked List - Tortoise and Hare.
9.21.2012
Detecting a Loop in Singly Linked List - Tortoise & Hare
This article is part of article series - "Datastructures"
Previous Article: Reversing a Singly Linked List.
Next Article: Finding first node in a Loop in Singly Linked List.
Eventhough there are multiple algorithms available we start with
Floyd's Cycle-Finding Algorithm
In simple terms it is also known as "Tortoise and Hare Algorithm" or "Floyd's Cycle Detection Algorithm" named after its inventor Robert Floyd. It is one of the simple cycle detection algorithm. It's a simple pointers based approach.
Previous Article: Reversing a Singly Linked List.
Next Article: Finding first node in a Loop in Singly Linked List.
Eventhough there are multiple algorithms available we start with
Floyd's Cycle-Finding Algorithm
In simple terms it is also known as "Tortoise and Hare Algorithm" or "Floyd's Cycle Detection Algorithm" named after its inventor Robert Floyd. It is one of the simple cycle detection algorithm. It's a simple pointers based approach.
Robert Floyd |
8.23.2012
Write a C Program without main function
In a C program main function is the entry point so it is mandatory to have a main function.
But let us see how can we write a program without main (Kind of hiding main in some obfuscated code). This post is purely out of interest to know that we can do something weird like this, just for learning.
But let us see how can we write a program without main (Kind of hiding main in some obfuscated code). This post is purely out of interest to know that we can do something weird like this, just for learning.
Subscribe to:
Posts
(
Atom
)