# 1. Introduction

This blog post delves into a common problem in data structures using C programming: identifying the intersection point of two singly linked lists. This challenge is pivotal in understanding linked list operations and is particularly relevant in contexts where data sets are interconnected.

## Problem

Given the heads of two singly linked lists, headA and headB, the task is to find and return the node where the two lists intersect. If there is no intersection, the function should return null. This problem tests our ability to manipulate and traverse linked lists.

# 2. Solution Steps

1. Traverse both lists to find their lengths.

2. Find the length difference between the two lists.

3. Advance the pointer in the longer list by the length difference.

4. Traverse both lists simultaneously until a common node is found or the end of either list is reached.

# 3. Code Program

``````#include <stdio.h>
#include <stdlib.h>

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

// Function to get the length of a linked list
int length = 0;
length++;
}
return length;
}

// Function to find the intersection node of two linked lists
int lengthA = getListLength(headA); // Length of list A
int lengthB = getListLength(headB); // Length of list B
int diff = abs(lengthA - lengthB); // Difference in lengths

// Advance the pointer in the longer list by the length difference
if (lengthA > lengthB) {
} else {
}

// Traverse both lists simultaneously to find the intersection
}
return NULL; // No intersection
}

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

// Main function to demonstrate getIntersectionNode function
int main() {
// Create two sample lists with intersection
// List A: 1->2->3
// List B: 4->5
// Intersection at node with value 3

if (result != NULL) {
printf("Output: Intersection at node with value %d\n", result->val); // Print the result
} else {
printf("Output: No intersection\n");
}

// Free the lists
free(headA->next); // Free rest of nodes in list A
free(headB->next); // Free nodes in list B (excluding intersection node)

return 0;
}
``````

### Output:

```Intersection at node with value 3
```

### Explanation:

The program first calculates the lengths of both linked lists.

It then advances the pointer in the longer list by the difference in lengths, so that both pointers are equidistant from the end of their respective lists.

It then simultaneously traverses both lists until a common node is found, indicating the intersection.

In the example, lists A (1->2->3) and B (4->5->3) intersect at the node with value 3. Hence, the output is 'Intersection at a node with value 3'.

This approach efficiently finds the intersection point without modifying the original lists.