Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does SpringBoot find out the cross nodes of two single linked lists

Shulou Source: shulou.com Published: 2022-06-01 11:24:55 09月21日 Update

This article mainly explains "SpringBoot how to find the cross nodes of two single linked lists". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "SpringBoot how to find the cross nodes of two single linked lists".

Topic: write a program to find out the cross nodes of two single linked lists.

Idea: single linked list An and single linked list B, the part after the intersection is the same, that is to say, the length is the same, as shown above: C1 → c2 → c3. Therefore, the difference between single linked list An and single linked list B can be removed, and the equal length parts can be compared in turn. After calculating the length of the two linked lists, compare whether the tail nodes of the two linked lists are the same. If different means there are no cross nodes, return NULL.

Language: c

/ * Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode* next; *}; * / struct ListNode* getIntersectionNode (struct ListNode* headA, struct ListNode* headB) {struct ListNode* curA = (struct ListNode*) malloc (sizeof (struct ListNode)); struct ListNode* curB = (struct ListNode*) malloc (sizeof (struct ListNode)); curA = headA; curB = headB;int length_a = 1 x int length_b = 1 x int I = 0 if (curA = NULL | | curB = = NULL) {return NULL } while (curA- > next! = NULL) {curA = curA- > next; length_a++;} while (curB- > next! = NULL) {curB = curB- > next; length_b++;} if (curA! = curB) {return NULL;} curA = headA; curB = headB;if (length_a > length_b) {for (I; I

< length_a-length_b; i++){ curA = curA->

Next;} I = 0;} else if (length_a

< length_b){for(i; i < length_b-length_a; i++){ curB = curB->

Next;} I = 0;} while (curA! = curB) {curA = curA- > next; curB = curB- > next;} return curA;}

Language: cpp

/ * Definition for singly-linked list. * struct ListNode {* int val; * ListNode * next; * ListNode (int x): val (x), next (NULL) {} *}; * / class Solution {public: ListNode * getIntersectionNode (ListNode * headA, ListNode * headB) {ListNode * curA, * curB; curA = headA; curB = headB;if (curA = = NULL | | curB = = NULL) {return NULL;} int length_a = getLength (curA) Int length_b = getLength (curB); if (length_a > length_b) {for (int iTuno; I)

< length_a-length_b; i++){ curA = curA->

Next;}} else if (length_a

< length_b){for(int i=0; i < length_b-length_a; i++){ curB = curB->

Next;}} while (curA! = curB) {curA = curA- > next; curB = curB- > next;} return curA;} private:int getLength (ListNode * head) {int length = 1scape while (head- > next! = NULL) {head = head- > next;length++;} return length;}}

Language:python

# Definition for singly-linked list.# class ListNode (object): # def _ init__ (self, x): # self.val = x # self.next = Noneclass Solution (object): def getIntersectionNode (self, headA, headB): ": type head1, head1: ListNode: rtype" if headA is None or headB is None:return Nonepa = headA # 2 pointerspb = headBwhile pa is not pb:# pa traverse headA first Then iterate through headB# pb, first headB, then headApa = headB if pa is None else pa.next pb = headA if pb is None else pb.nextreturn pa # there are only two ways to end the loop, one is that pa and pb refer to the same, and the other is that both headA and headB have been traversed and still can't be found. Thank you for your reading, the above is the content of "SpringBoot how to find the cross nodes of two single linked lists". After the study of this article, I believe you have a deeper understanding of how SpringBoot finds the cross nodes of two single linked lists, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Two nodes intersections parts learning content ideas length the same that is that is the intersection only as above is the situation refers to articles ways more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL NVidia Xiaomi Apple