In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to find the first common node of the two linked lists on the leetcode linked list. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Title
Enter two linked lists to find their first common node.
Such as the following two linked lists: intersect at node C1.
Example 1: input: intersectVal = 8, listA = [4Jing 1Jing 8Jing 4Jing 5], listB = [5Jing 0Jing 8Jet 4Jing 5], skipA = 2, skipB = 3 output: Reference of the node with value = 8 input explanation: the value of the intersecting node is 8 (note that it cannot be 0 if two lists intersect). Starting from their respective headers, the linked list An is [4 magi 1, 8, 4, 5], and the linked list B is [5, 0, 1, 1, 8, 4, and 5]. In A, there are 2 nodes in front of intersecting nodes; in B, there are 3 nodes in front of intersecting nodes.
Example 2: input: intersectVal = 2, listA = [0je 9pai 1pi 2jue 4], listB = [3Jing 2jue 4], skipA = 3, skipB = 1 output: Reference of the node with value = 2 input explanation: the value of the intersecting node is 2 (note that it cannot be 0 if two lists intersect). Starting from their respective headers, the linked list An is [0pje 9pr 1pr 2pr 4], and the linked list B is [3je 2pr 4]. In A, there are 3 nodes before the intersecting nodes; in B, there is 1 node before the intersecting nodes.
Example 3: input: intersectVal = 0, listA = [2Jing 6jue 4], listB = [1Magi 5], skipA = 3, skipB = 2 output: null input explanation: starting from their respective headers, linked list An is [2Jing 6jue 4], linked list B is [1jue 5]. Because the two linked lists do not intersect, intersectVal must be 0, and skipA and skipB can be any value. Explanation: the two linked lists do not intersect, so null is returned.
Note:
If the two linked lists do not intersect, return null. After the results are returned, the two linked lists must remain in their original structure. It can be assumed that there are no loops in the entire linked list structure. The program satisfies O (n) time complexity as far as possible, and only O (1) memory is used. This question is the same as the 160th question on the main site: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
Source: LeetCode link: https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source.
Answer the question / Definition for singly-linked list. * public class ListNode {* int val; * ListNode next; * ListNode (int x) {* val = x; * next = null; *} * * / public class Solution {public ListNode getIntersectionNode (ListNode headA, ListNode headB) {ListNode C1 = headA; ListNode c2 = headB; while (C1! = c2) {C1 = C1 = = null? HeadB: c1.nextc2 = c2 = = null? HeadA: c2.next;} return C1;}} this is the end of the article on "how to find the first common node of two linked lists". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.