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;
     
    
}
相关推荐
晨非辰1 小时前
《剑指Offer:单链表操作入门——从“头删”开始破解面试》
c语言·开发语言·数据结构·c++·笔记·算法·面试
啊我不会诶4 小时前
24ICPC成都站补题
数据结构·算法
仟千意5 小时前
数据结构:栈和队列
数据结构
渡我白衣5 小时前
list 与 forward_list:一场 STL 中的“链表哲学”之争
数据结构·c++·list
Pocker_Spades_A11 小时前
【C语言数据结构】第2章:线性表(2)--线性表的顺序存储结构
c语言·数据结构
小许学java14 小时前
七大排序算法的基本原理
数据结构·算法·排序算法
_dindong16 小时前
动规:01背包
数据结构·笔记·学习·算法·leetcode·动态规划·力扣
-雷阵雨-16 小时前
数据结构——排序算法全解析(入门到精通)
java·开发语言·数据结构·排序算法·intellij-idea
LGL6030A17 小时前
数据结构学习(1)——指针、结构体、链表(C语言)
数据结构·学习
蒙奇D索大17 小时前
【数据结构】考研算法精讲:分块查找的深度剖析 | 从“块内无序、块间有序”思想到ASL性能最优解
数据结构·笔记·学习·考研·改行学it