Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report