Sword refers to the offer face question (Java version): The first public node of two linked list __ Sword refers to offer (Java edition)

Source: Internet
Author: User

Title: Enter two lists to find their first public node.

During the interview, the first reaction of many candidates was brute force method: In order to traverse each node in the first linked list, without traversing to a node, the second linked list in order to traverse each node. If there is a node on the second list that is the same as the node on the first list, it means that the two lists overlap at the node, so they find their common node. If the length of the first linked list is m and the length of the second list is n, it is obvious that the method has a time complexity of O (MN).

Usually brute force is not the best way to do it, so we'll try to analyze the characteristics of the two linked lists with public nodes. From the definition of the linked list node, we can see that the two linked lists are single linked lists. If two one-way linked lists have public nodes, then the two linked lists from a node, their m_pnext all point to the same node, but because it is a one-way list of nodes, after all their nodes are coincident, it is impossible to appear bifurcation. So there are two of linked lists with common nodes and partially coincident, and topological shapes open up like a Y, not an item X.


After analysis we found that if two linked lists have public nodes, then common nodes appear at the end of two lists. If we start by comparing the tails of the two lists, the last identical node is the one we're looking for. The problem is in a one-way list, we can only start from the beginning of the node in order to traverse, finally to reach the tail node. The last node that arrives is compared to the first, which sounds like a LIFO, we can think of using the characteristics of the stack to solve this problem: the two linked list of nodes into two stacks, so that the end of the two linked list is located in the top two stack, and then compare two stack top node is the same. If the same is the case, pop the top of the stack and compare the top of the stack until you find the last identical node.

In the above ideas, we need to use two auxiliary stacks. If the length of the list is M and N, then the space complexity is O (m+n). The time complexity of this idea is also O (m+n). Compared with the first brute force method, time efficiency has been improved, which is equivalent to the use of space consumption in exchange for time efficiency.

The reason we need to use the stack is because we want to traverse the tail node that reaches two stacks at the same time. When the length of two linked lists is not the same, it is not consistent if we start from the beginning to traverse the tail node of the two stacks. In fact, there is a simpler way to solve this problem: first traverse two of the list to get their length, you can know which linked list is longer, and long linked list than a short list more than a few nodes. In the second traversal, take a few steps on the longer list, then iterate over the two linked lists, finding the first common node is their first.

The third idea is that the time complexity is O (m+n) compared to the second thought, but we do not need the auxiliary stack, thus improving the space efficiency. When the interviewer affirms our last thought, we can write the code.

Java Code Implementation:

/** * Enter two single linked lists to find their first public node.

* * Package swordforoffer; Import Utils.
ListNode; /** * @author Jinshuangqi * * * August 9, 2015 * * public class E37firstcommonnodesinlists {public ListNode Findfirstcommon
		Node (ListNode root1,listnode root2) {int length1 = GetLength (ROOT1);
		int length2 = GetLength (ROOT2);
		ListNode pointlonglistnode = null;
		ListNode pointshortlistnode = null;
		int dif = 0;
			if (length1 >length2) {pointlonglistnode = ROOT1;
			Pointshortlistnode = Root2;
		dif = length1-length2;
			}else{Pointlonglistnode = Root2;
			Pointshortlistnode = ROOT1;
		dif = length2-length1;
		for (int i = 0;i<dif;i++) Pointlonglistnode = Pointlonglistnode.next; while (Pointlonglistnode!= null && pointshortlistnode!= null && pointlonglistnode!= Pointshortlistnode
			) {Pointlonglistnode = Pointlonglistnode.next;
		Pointshortlistnode = Pointshortlistnode.next;
	return pointlonglistnode; private int getlength (ListNode root) {int result =0;
		if (root = null) return result;
		ListNode point = root;
			while (point!= null) {point = Point.next;
		result++;
	return result;
 }
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.