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;
     
    
}
相关推荐
leonkay7 小时前
Golang语言闭包完全指南
开发语言·数据结构·后端·算法·架构·golang
casual~8 小时前
第?个质数(埃氏筛算法)
数据结构·c++·算法
仰泳的熊猫9 小时前
题目2308:蓝桥杯2019年第十届省赛真题-旋转
数据结构·c++·算法·蓝桥杯
hssfscv9 小时前
力扣练习训练2(java)——二叉树的中序遍历、对称二叉树、二叉树的最大深度、买卖股票的最佳时机
java·数据结构·算法
不想看见40410 小时前
Search a 2D Matrix II数组--力扣101算法题解笔记
数据结构·算法
IronMurphy10 小时前
【算法二十六】108. 将有序数组转换为二叉搜索树 98. 验证二叉搜索树
数据结构·算法·leetcode
InfiniSynapse11 小时前
连上Snowflake就能取数:InfiniSynapse + Spider2-Snow实战企业数据分析
数据结构·图像处理·人工智能·算法·语言模型·数据挖掘·数据分析
WolfGang00732112 小时前
代码随想录算法训练营 Day13 | 二叉树 part03
数据结构·算法·leetcode
91刘仁德12 小时前
C++ 内存管理
android·c语言·数据结构·c++·经验分享·笔记·算法
历程里程碑12 小时前
链表-----
数据结构·线性代数·算法·链表·矩阵·lua·perl