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;
     
    
}
相关推荐
半盏茶香18 分钟前
扬帆数据结构算法之雅舟航程,漫步C++幽谷——LeetCode刷题之移除链表元素、反转链表、找中间节点、合并有序链表、链表的回文结构
数据结构·c++·算法
DARLING Zero two♡1 小时前
【初阶数据结构】逆流的回环链桥:双链表
c语言·数据结构·c++·链表·双链表
带多刺的玫瑰1 小时前
Leecode刷题C语言之从栈中取出K个硬币的最大面积和
数据结构·算法·图论
Cando学算法1 小时前
Codeforces Round 1000 (Div. 2)(前三题)
数据结构·c++·算法
秋风&萧瑟3 小时前
【数据结构】顺序队列与链式队列
linux·数据结构·windows
sci_ei12316 小时前
高水平EI会议-第四届机器学习、云计算与智能挖掘国际会议
数据结构·人工智能·算法·机器学习·数据挖掘·机器人·云计算
qystca17 小时前
异或和之和
数据结构·c++·算法·蓝桥杯
周杰伦_Jay18 小时前
Ollama能本地部署Llama 3等大模型的原因解析(ollama核心架构、技术特性、实际应用)
数据结构·人工智能·深度学习·架构·transformer·llama
萌の鱼18 小时前
leetcode 221. 最大正方形
数据结构·c++·算法·leetcode
Joeysoda20 小时前
Java数据结构 (链表反转(LinkedList----Leetcode206))
java·linux·开发语言·数据结构·链表·1024程序员节