首页 > 其他 > 详细

leetcode-剑指52-OK

时间:2021-01-22 22:47:36      阅读:54      评论:0      收藏:0      [点我收藏+]

address

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    int getlenth(struct ListNode *head){
	    int len = 0;
	    while(head !=NULL){
	    	len++;
	    	head = head->next;
	    }
	    return len;
	}
	int lenA = getlenth(headA);
	int lenB = getlenth(headB);
	if (lenA>lenB){
		int cha = lenA-lenB;
		while(cha>0){
			headA = headA->next;
			cha--;
		}
		lenA = lenB;
	}
	if (lenA<lenB){
		int cha = lenB-lenA;
		while(cha>0){
			headB = headB->next;
			cha--;
		}
		lenB = lenA;
	}
// 至此,两个头已经一样了
	while(headA && (headA !=headB)){
		headB = headB->next;
		headA = headA->next;
	}
	return headA;
}

leetcode-剑指52-OK

原文:https://www.cnblogs.com/gallien/p/14315079.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!