想成为Google工程师?先回答这15个面试问题【这只是一必要条件】(二)

       第二题,合并两个有序链表。

      

2.合并两个排序链表
挑战: 这类问题是Google面试问题的一个共同趋势:找出解决问题的有效办法。

合并两条链表是一般会在链表之间发生“冲突”(因为它们各自有特定的次序,而你的合并会把次序搞乱)

你必须找出一种算法快速消除那些冲突。
       不是很理解它里面所谓的”冲突“,是否包含着其他玄机,^_^,先用简单的写法写一个吧,不然,合并两个升序的链表。

//program used to merge two  sort list
#include<iostream>
#include<fstream>

using namespace std;

ofstream fout("output.txt");

typedef struct Node
{
	int data;
	Node * next;
} *List;

List list1 = NULL;
List list2 = NULL;

//generate the two list
void generateList()
{
	int data1[10] = {2, 4, 8, 10, 12, 14, 18, 19, 30, 50};
	int data2[8] = { 3, 5, 9, 10, 14, 28, 34, 59 };
	
	for (int i=9; i >= 0; i --)
	{
		Node * temp = new Node();
		temp->data = data1[i];
		temp->next = list1;
		list1 = temp;
	}//end for data1
	
	for (int i=7; i>=0; i--)
	{
		Node * temp = new Node();
		temp->data = data2[i];
		temp->next = list2;
		list2 = temp;
	}//end for data2
}

//merge the two sort list
List mergesort(List list1, List list2)
{
	List result, cur;

	//initial the head of the list
	if (list1->data > list2->data)
	{
		result = list2;
		cur = list2;
		list2 = list2->next;
	}
	else
	{
		result = list1;
		cur = list1;
		list1 = list1->next;
	}//end if ... else 

	while(list1 != NULL && list2 != NULL)
	{
		if (list1->data > list2->data)
		{
			cur->next = list2;
			cur = list2;
			list2 = list2->next;
		}
		else
		{
			cur->next = list1;
			cur = list1;
			list1 = list1->next;
		}
	}//END WHILE
	return result;
}

//print the list
void print(List list)
{
	while(list)
	{
		cout << list->data << " ";
		list= list->next;
	}
	cout << endl;
}

int main()
{
	//generate the list
	generateList();

	//print the list
	print(list1);
	print(list2);

	//merge the sort list
	List m = mergesort(list1, list2);

	//print the result
	print(m);
	return 0;
}
     这个还是比较简单的,比较难写的是单链表快排,这是以前写的一个单链表快排,一并附上作为一个参考吧。

#include <iostream>
#include <stdlib.h>  


using namespace std;

struct Node
{
	int data;
	Node* next;
};


Node * generate()
{
	Node * head = (Node *)malloc(sizeof(Node));
	head->data = rand()%100 + 1;
	head->next = NULL;

	for (int i=0; i < 10; i ++)
	{	
		Node * temp = (Node*)malloc(sizeof(Node));
		temp->data = rand()%100 + 1;
		temp->next = head->next;
		head->next = temp;
	}
	return head;
}

void printList(Node * head)
{
	while (head != NULL)
	{
		cout << head->data << " ";
		head = head->next;
	}
	cout << endl;
}

Node * quicksortpass(Node **start, Node* end)
{
	Node * provit = *start;
	Node * left = provit;
	Node * right = provit;

	(*start) = (*start)->next;
	while((*start) != end){
		Node * temp = (*start);
		(*start) = (*start)->next;
		if(temp->data < provit->data)
		{
			temp->next = left;
			left = temp;
		}
		else
		{
			right->next = temp;
			right = temp;
		}
	}
	right->next = end;
	*start = left;	
	return provit;
}

void quicksortlist(Node ** start, Node * end )
{
	if(*start != end)
	{
		Node * provit = quicksortpass(start, end);
		quicksortlist(start, provit);
		quicksortlist(&(provit->next), end);
	}
}

int main()
{
	Node * list = generate();
	printList(list);
	
	quicksortlist(&list, NULL);
	printList(list);

	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值