Leetcode_160_Intersection of Two Linked Lists

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pistolove/article/details/41593747


Intersection of Two Linked Lists 

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2
                     c1 → c2 → c3
B:     b1 → b2 → b3

begin to intersect at node c1.











public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
	int lenA = 0;
	int lenB = 0;
	ListNode A = headA;
	ListNode B = headB;

	if (A == null || B == null)
		return null;

	while (A.next != null) {
		A = A.next;


	while (B.next != null) {
		B = B.next;

	// 如果有环状的,终点会不一样,返回null
	if (A != B)  return null;

	A = headA;
	B = headB;
	// 找到相差的个数,并移动到相同的地方开始比较
	if (lenA > lenB) {
		for (int i = 0; i < lenA - lenB; i++) {
			A = A.next;
	} else {
		for (int i = 0; i < lenB - lenA; i++) {
			B = B.next;

	// 如果不相等则比较其后续是否相等
	while (A != B) {
		A = A.next;
		B = B.next;
	return A;

two problems about linked list


who can solve these two problems?try these if you have the knowledge of data structure for c++.solve it in c++,please. rnrnProblem 1 .rnImplement a sorting algorithm using a singly linked list. Thernalgorithm (known as the Insertion sort algorithm) works as follows:rn1. It starts with an empty linked list.rn2. When a first node is inserted to the list, the node is inserted at the head of the list.rn3. To insert a new node, the algorithm traverses the linked list until it finds a specificrnposition to insert the new node by comparing the new node’s data value to eachrnexisting node’s data value.rn4. Traverses all the elements of the linked list to print out the sorted values. [optional]rn5. Repeats steps 3 and 4 for each and every new node.rnImplement the insertion sort algorithm in C++ to ascendingly sort integers. Your programrnshould receive an arbitrary number of input integers one by one until you interrupt or stoprnyour program (by pressing Ctrl-C, for example). For each input integer, the program printsrnout the sorted list after inserting the value. The screen shot of your program may look like thernfollowing:rn$ a.outrn== Insertion sort by [your name] ==rn[Empty List]rnInput an integer: 5rnSorted List : 5rnInput an integer: 7rnSorted List: 5 7rnInput an integer: 2rnSorted List: 2 5 7rnInput an integer: 6rnSorted List: 2 5 6 7rnInput an integer: 3rnSorted List: 2 3 5 6 7rnAlso answer the following questions (as the comments at the end of your C++ program) :rn� Explain the input sequences that will result in the best and the worst algorithmicrnperformances, in terms of the number of comparisons required.rn� If there are N input values to be sorted, what is the best case complexity and what is thernworse case complexity, in terms of N ?rn rnrnProblem 2 rn Implement the doubly circular linked list for integers in C++. Provide the following DCLL operations as the method members of the DCLL class :rn1. Add a node at the head of the listrn2. Add a node at the tail of the list (i.e. at head->previous)rn3. Add a node after a specific n-th node (e.g. after the 4-th node). If such node does notrnexist, simply deny the operationrn4. Delete a node from the head of the listrn5. Delete a node from the tail of the listrn6. Delete a specific n-th node (e.g. delete the 5-th node). If such node does not exist, simplyrndeny the operation.rn7. Delete all the nodes that have a specific value (i.e. yes, there can be duplicate nodes)rn8. Determine if the list contain a particular value and how many duplicate nodes there arern9. Traverse the list in a forward direction (i.e. from head to tail)rn10. Traverse the list in a backward direction (i.e. from tail to head)rnCreate a user interface in the main() function that is similar to the singly linked list (sll.cc)rnexample given on the class web page. The user interface should provide the above DCLLrnoperations appearing in such order. After applying each operation, your DCLL must stillrnremain consistent, i.e. all the pointers point to where they are supposed to.rn


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他