Leetcode_160_Intersection of Two Linked Lists

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

本文是在学习中的总结,欢迎转载但请注明出处:http://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.


思路:

(1)这道算法题是一道较老的题目。记得在数据结构书的习题中出现过。题目不难,但有几个点需要注意。

(2)需要注意:A:链表为空的判断;B:链表是否带有环的判断;C:两链表长度之差处的起始点的寻找。

(3)首先,对链表为空的判断,为空则返回null;

(4)其次,需设置两个临时变量A和B分别指向两链表的起始位置,分别遍历两链表并求得其长度,如果遍历完成A和B指向的不 

         是同一节点,则说明链表带有环,返回null;

(5)再次,比较两链表的大小,将较大的链表从其起始位置向后移动到两链表长度之差的位置,这样,两链表就出于同一起始位

         置了;

(5)最后,判断起始位置对应节点是否相同,不相同则两节点分别后移,最后返回A或B即为结果。


算法代码实现如下所示(PS:题目要求时间复杂度为O(n),空间复杂度为O(1),大家有更好的算法希望能分享,谢谢):

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) {
		lenA++;
		A = A.next;

	}

	while (B.next != null) {
		lenB++;
		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

07-19

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

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试