leetcode160题相交链表

cs 复制代码
#include <stdio.h>
#include <stdlib.h> 
struct Node{
	int data;
	struct Node* next;
};

int List_length(struct Node* head)
{
	int length = 0;
	while(head != NULL)
	{
		length ++;
		head = head->next;
	}
	return length;
}

struct Node* move_pointer(struct Node* head, int count)
{
	int i;
	for(i = 0; i < count; i ++)
	{
		head = head->next;
	}
	return head;
}

struct Node *getIntersectionNode(struct Node* headA, struct Node* headB)
{
	int lengthA = List_length(headA);
	int lengthB = List_length(headB);
	
	if(lengthA > lengthB){
		headA = move_pointer(headA, lengthA - lengthB);
	}else{
		headB = move_pointer(headB, lengthB - lengthA);
	}
	
	struct Node* result = NULL;
	while(headA != NULL && headB != NULL)
	{
		if(headA == headB){
			result = headA;
			break;
		}
		headA = headA->next;
		headB = headB->next;
	}
	return result;
}

void freeList(struct Node* head) {
    struct Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

void Print(struct Node* head)
{
	struct Node *temp = head;
	while(temp != NULL){
		printf("%d ",temp->data);
		temp = temp->next;
	}
	printf("\n");
}



int main()
{
    // 创建示例链表
    struct Node* headA = (struct Node*)malloc(sizeof(struct Node));
    struct Node* headB = (struct Node*)malloc(sizeof(struct Node));
    struct Node* intersection = (struct Node*)malloc(sizeof(struct Node));
    
    // 初始化链表A:4 -> 1 -> 8 -> 4 -> 5
    headA->data = 4;
    headA->next = (struct Node*)malloc(sizeof(struct Node));
    headA->next->data = 1;
    headA->next->next = intersection;
    
    // 初始化链表B:5 -> 6 -> 1 -> 8 -> 4 ->5
    headB->data = 5;
    headB->next = (struct Node*)malloc(sizeof(struct Node));
    headB->next->data = 6;
    headB->next->next = (struct Node*)malloc(sizeof(struct Node));
    headB->next->next->data = 1;
    headB->next->next->next = intersection;
    
    // 初始化相交节点
    intersection->data = 8;
    intersection->next = (struct Node*)malloc(sizeof(struct Node));
    intersection->next->data = 4;
    intersection->next->next = (struct Node*)malloc(sizeof(struct Node));
    intersection->next->next->data = 5;
    intersection->next->next->next = NULL;
    
    Print(headA);
    Print(headB);
    
    
 	// 查找相交节点
    struct Node* result = getIntersectionNode(headA, headB);
    
    if (result) {
        printf("相交节点的数据为:%d\n", result->data);
    } else {
        printf("没有相交节点\n");
    }
    
    freeList(headA);
    freeList(headB); // 释放链表B的内存
    freeList(intersection); // 释放相交部分的内存
    
    return 0;
     
    
}
相关推荐
liu****2 小时前
10.queue的模拟实现
开发语言·数据结构·c++·算法
shinelord明3 小时前
【大数据技术实战】Kafka 认证机制全解析
大数据·数据结构·分布式·架构·kafka
让我们一起加油好吗3 小时前
【基础算法】01BFS
数据结构·c++·算法·bfs·01bfs
草莓工作室3 小时前
数据结构3:线性表2-顺序存储的线性表
数据结构·windows
1白天的黑夜14 小时前
递归-24.两两交换链表中的节点-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
1白天的黑夜14 小时前
递归-206.反转链表-力扣(LeetCode)
数据结构·c++·leetcode·链表·递归
靠近彗星4 小时前
3.1 栈
数据结构·算法
sulikey5 小时前
一文彻底理解:如何判断单链表是否成环(含原理推导与环入口推算)
c++·算法·leetcode·链表·floyd·快慢指针·floyd判圈算法
孤廖6 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
豆沙沙包?6 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表