-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: add Two-Pointer Technique for linked lists #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 1 commit into
TheAlgorithms:master
from
Brijesh03032001:add-two-pointer-only
Oct 26, 2025
+134
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # Two-Pointer Technique (Fast and Slow Pointers / Tortoise and Hare) | ||
| # | ||
| # Template and practical application for singly linked lists using the | ||
| # fast & slow pointer technique. This file provides: | ||
| # - A minimal Node reference-class local to this file | ||
| # - Generic helper: build list from a vector | ||
| # - detect_cycle(head): returns TRUE/FALSE using tortoise & hare | ||
| # - find_middle(head): returns the middle node's data (for even length returns the second middle) | ||
| # - nth_from_end(head, n): returns the data of the Nth node from the end (1-based) | ||
| # | ||
| # Practical application demonstrated: finding the Nth node from the end. | ||
|
|
||
| TwoPtrNode <- setRefClass("TwoPtrNode", | ||
| fields = list( | ||
| data = "ANY", | ||
| next_node = "ANY" | ||
| ), | ||
| methods = list( | ||
| initialize = function(data = NULL, next_node = NULL) { | ||
| .self$data <- data | ||
| .self$next_node <- next_node | ||
| }, | ||
|
|
||
| print = function() { | ||
| cat("Node(data =", .self$data, ")\n") | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| # Build a singly linked list from an R vector of values. Returns the head node. | ||
| build_list_from_vector <- function(vec) { | ||
| if (length(vec) == 0) return(NULL) | ||
| head <- TwoPtrNode$new(data = vec[1]) | ||
| current <- head | ||
| if (length(vec) > 1) { | ||
| for (v in vec[-1]) { | ||
| new_node <- TwoPtrNode$new(data = v) | ||
| current$next_node <- new_node | ||
| current <- new_node | ||
| } | ||
| } | ||
| return(head) | ||
| } | ||
|
|
||
| # Convert linked list to vector for easy printing/inspection | ||
| # This is defensive against cycles by truncating after `max_nodes` items. | ||
| list_to_vector <- function(head, max_nodes = 1000) { | ||
| out <- c() | ||
| cur <- head | ||
| i <- 0 | ||
| while (!is.null(cur) && i < max_nodes) { | ||
| out <- c(out, cur$data) | ||
| cur <- cur$next_node | ||
| i <- i + 1 | ||
| } | ||
| if (!is.null(cur)) { | ||
| out <- c(out, "... (truncated or cycle detected)") | ||
| } | ||
| return(out) | ||
| } | ||
|
|
||
| # Detect cycle using fast and slow pointers (Tortoise & Hare) | ||
| detect_cycle <- function(head) { | ||
| if (is.null(head)) return(FALSE) | ||
| slow <- head | ||
| fast <- head | ||
| while (!is.null(fast) && !is.null(fast$next_node)) { | ||
| slow <- slow$next_node | ||
| fast <- fast$next_node$next_node | ||
| if (identical(slow, fast)) return(TRUE) | ||
| } | ||
| return(FALSE) | ||
| } | ||
|
|
||
| # Find middle node using two pointers. For even-length lists this returns | ||
| # the second middle (i.e., for 1->2->3->4 it returns 3). | ||
| find_middle <- function(head) { | ||
| if (is.null(head)) return(NULL) | ||
| slow <- head | ||
| fast <- head | ||
| while (!is.null(fast) && !is.null(fast$next_node)) { | ||
| slow <- slow$next_node | ||
| fast <- fast$next_node$next_node | ||
| } | ||
| return(slow) | ||
| } | ||
|
|
||
| # Return the data of the Nth node from the end (1-based). Throws an error | ||
| # if n is invalid or greater than list length. | ||
| nth_from_end <- function(head, n) { | ||
| if (is.null(head)) stop("List is empty") | ||
| if (n <= 0) stop("n must be a positive integer") | ||
|
|
||
| fast <- head | ||
| # Advance fast by n steps | ||
| for (i in seq_len(n)) { | ||
| if (is.null(fast)) stop(sprintf("n (%d) is larger than the list length", n)) | ||
| fast <- fast$next_node | ||
| } | ||
|
|
||
| slow <- head | ||
| # Move both until fast is NULL; slow will be at the Nth from end | ||
| while (!is.null(fast)) { | ||
| slow <- slow$next_node | ||
| fast <- fast$next_node | ||
| } | ||
| return(slow$data) | ||
| } | ||
|
|
||
| # ---- Practical demonstration ---- | ||
| if (sys.nframe() == 0) { | ||
| cat("Two-Pointer Technique demo\n") | ||
| head <- build_list_from_vector(1:7) | ||
| cat("List:", paste(list_to_vector(head), collapse = " -> "), "\n") | ||
|
|
||
| mid <- find_middle(head) | ||
| cat("Middle node data:", ifelse(is.null(mid), "NULL", mid$data), "\n") | ||
|
|
||
| n <- 2 | ||
| nth <- nth_from_end(head, n) | ||
| cat(sprintf("%d-th node from the end: %s\n", n, nth)) | ||
|
|
||
| cat("Cycle detected?", detect_cycle(head), "\n") | ||
|
|
||
| # Create a cycle for testing (connect tail to node with data=3) | ||
| tail <- head | ||
| while (!is.null(tail$next_node)) tail <- tail$next_node | ||
| cur <- head | ||
| while (!is.null(cur) && cur$data != 3) cur <- cur$next_node | ||
| if (!is.null(cur)) tail$next_node <- cur | ||
| cat("After creating a cycle (tail -> node with data 3):\n") | ||
| cat("Cycle detected?", detect_cycle(head), "\n") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.