Skip to content
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

added intersection of two linked list #1581 #1651

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Linked_list/Intersection_of_Two_Linked_List/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>

struct ListNode {
int val;
struct ListNode *next;
};

// Helper function to create a new node
struct ListNode* createNode(int value) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = value;
newNode->next = NULL;
return newNode;
}

// Function to find the intersection node of two linked lists
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
if (!headA || !headB) return NULL;

struct ListNode* ptrA = headA;
struct ListNode* ptrB = headB;

// Traverse both lists; when one pointer reaches the end, start from the beginning of the other list
while (ptrA != ptrB) {
ptrA = (ptrA == NULL) ? headB : ptrA->next;
ptrB = (ptrB == NULL) ? headA : ptrB->next;
}

// Either both pointers meet at the intersection node, or they meet at NULL
return ptrA;
}

// Helper function to print the linked list
void printList(struct ListNode* head) {
while (head != NULL) {
printf("%d -> ", head->val);
head = head->next;
}
printf("NULL\n");
}

int main() {
struct ListNode* listA = createNode(4);
listA->next = createNode(1);
struct ListNode* intersectNode = createNode(9);
listA->next->next = intersectNode;
intersectNode->next = createNode(4);
intersectNode->next->next = createNode(5);

struct ListNode* listB = createNode(5);
listB->next = createNode(6);
listB->next->next = createNode(1);
listB->next->next->next = intersectNode;

printf("List A: ");
printList(listA);
printf("List B: ");
printList(listB);

struct ListNode* intersection = getIntersectionNode(listA, listB);

if (intersection) {
printf("Intersected at '%d'\n", intersection->val);
} else {
printf("No intersection\n");
}

return 0;
}
35 changes: 35 additions & 0 deletions Linked_list/Intersection_of_Two_Linked_List/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Intersection of Two Linked Lists

## Problem Statement

Given the heads of two singly linked-lists, `headA` and `headB`, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return `NULL`.

### Example

**Input**:
- `listA = [4,1,8,4,5]`
- `listB = [5,6,1,8,4,5]`

**Output**:
- `Intersected at '8'`

**Explanation**:
- Both lists intersect at the node with value `8`.

### Constraints
- Each linked list is non-cyclical.
- The intersection, if it exists, is a reference to a node, not a new node with the same value.

## Approach

### Two-Pointer Technique

1. **Set up two pointers,** `ptrA` starting at the head of `listA` and `ptrB` starting at the head of `listB`.
2. **Traverse the lists**:
- If a pointer reaches the end of one list, redirect it to the head of the other list.
- Continue moving both pointers forward one node at a time until they meet.
3. **Why This Works**:
- When the lists intersect, both pointers will meet at the intersection node after traversing the combined length of both lists.
- If they don’t intersect, both pointers will eventually reach `NULL` at the same time.

This method is efficient with a time complexity of `O(m + n)`, where `m` and `n` are the lengths of `listA` and `listB`. It also has `O(1)` space complexity as it modifies the list in place without additional data structures.
Loading