C++面试——链表

链表逆序

#include
using namespace std;

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

ListNode * reverseList(ListNode* head) {
	ListNode *p, *q, *t=nullptr;
	p = head;
	if (p->next == NULL || p->next->next == NULL) {
		return head;
	}
	p = head->next;
	q = head->next->next;
	while (q != nullptr) {
		t = q->next;
		q->next = p;
		p = q;
		q = t;
	}
	head->next->next = nullptr;
	head->next = p;
	return head;
}

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

int main() {
	ListNode *head=new ListNode;
	head->data = -1;
	head->next = nullptr;
	int x;
	ListNode*p;
	p = head;
	cin >> x;
	while (x!=-1) {
		ListNode *q = new ListNode;
		q->data = x;
		q->next = NULL;
		p->next = q;
		p = q;
		cin >> x;
	}
	printList(head);
	printList(reverseList(head));
	return 0;
}

合并两个有序链表
递归法

#include
using namespace std;

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

ListNode*  createList(ListNode* head) {
	int x;
	ListNode *p, *q = nullptr;
	p = head;
	cin >> x;
	while (x != -1) {
		q = new ListNode;
		q->data = x;
		q->next = nullptr;
		p->next = q;
		p = q;
		cin >> x;
	}
	return head;
}

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

ListNode* mergeTwoLists(ListNode* L1,ListNode* L2) {
	if (L1 == nullptr)
		return L2;
	if (L2 == nullptr)
		return L2;
	if (L1->data < L2->data) {
		L1->next = mergeTwoLists(L1->next, L2);
		return L1;
	}
	else
	{
		L2->next = mergeTwoLists(L1, L2->next);
		return L2;
	}
}

int main() {
	ListNode *head=new ListNode;
	head->data = -1;
	head->next = nullptr;	
	head = createList(head);
	printList(head);
	printList(reverseList(head));
	ListNode *L1 = new ListNode;
	ListNode *L2 = new ListNode;
	L1 = createList(L1);
	L2 = createList(L2);
	printList(mergeTwoLists(L1, L2)->next);
	return 0;
}

合并两个有序链表
非递归

#include
using namespace std;


//链表节点
struct ListNode {
	int data;
	ListNode* next;
};

//打印链表
void printList(ListNode* head) {
	head = head->next;
	while (head != nullptr) {
		cout << head->data << " ";
		head = head->next;
	}
	cout << endl;
}

//构建链表
ListNode* createList(ListNode* head) {
	ListNode *p, *q;
	int x;
	p = head;
	cin >> x;
	while (x != -1) {
		q = new ListNode;
		q->data = x;
		q->next = nullptr;
		p->next = q;
		p = q;
		cin >> x;
	}
	return head;
}

//链表逆序
ListNode* reverseList(ListNode *head) {
	ListNode *p = head;
	ListNode *q, *t=nullptr;
	if (p->next == nullptr || p->next->next == nullptr)
		return head;
	p = head->next;
	q = head->next->next;
	while (q != nullptr) {
		t = q->next;
		q->next = p;
		p = q;
		q = t;
	}
	head->next->next = nullptr;
	head->next = p;
	return head;
}

//合并链表
ListNode* mergeTwoLists(ListNode* L1, ListNode* L2) {
	if (L1 == nullptr)
		return L2;
	if (L2 == nullptr)
		return L1;
	if (L1->data < L2->data) {
		L1->next = mergeTwoLists(L1->next, L2);
		return L1;
	}
	else {
		L2->next = mergeTwoLists(L1, L2->next);
		return L2;
	}
}

int main() {
	ListNode *head = new ListNode;
	head->data = -1;
	head->next = nullptr;
	head = createList(head);
	printList(head);
	head = reverseList(head);
	printList(head);
	ListNode *L1, *L2;
	L1 = new ListNode;
	L2 = new ListNode;
	L1 = createList(L1);
	L2 = createList(L2);
	printList(mergeTwoLists(L1, L2)->next);
}

你可能感兴趣的:(工作)