Sword point offer -- reverse linked list and merge two linked lists that can be sorted

Posted by crazy/man on Fri, 01 Nov 2019 01:53:36 +0100

The beginning: this series is based on the offer of the sword finger of niuke.com. The purpose is to understand and communicate, focusing on the record. I hope you can give me some advice!

Niuke - Sword finger offer

Article directory

1. Reverse list

Description: input a linked list, reverse the linked list, output the header of the new linked list.

Idea 1: see code notes;

Test code:

#include <algorithm>

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) :val(x), next(nullptr) {}
};

class Solution {
public:
	ListNode* ReverList(ListNode* pHead) {
		//Program robustness
		if (pHead == NULL)
			return NULL;
		//
		ListNode* pNode = pHead;	//Current head pointer
		ListNode* pReverseHead = NULL;	//Head pointer of new linked list
		ListNode* pPrev = NULL;	 //Previous node of current pointer

		while (pNode!=NULL){	//Execute when the current node is not empty
			ListNode* pNext = pNode->next;	//Make sure to save the nodes behind the disconnection position before the chain is disconnected

			if (pNext == NULL) {
				pReverseHead = pNode;	//When pNext is empty, it means that the node is the tail node, which is the header of the new linked list.
			}

			pNode->next = pPrev;	//Reversal only
			pPrev = pNode;	//Current node to previous
			pNode = pNext;	//Next for the present
		}

		return pReverseHead;
	}
};

Train of thought 2: recursive method
Use recursion to go to the end of the list, and then update the nextnextnext value of each nodenode node to realize the inversion of the list. However, the value of newheadnewheadnewhead has not changed. It is the last node of the linked list. Therefore, after reversing, we can get the headhead of the new linked list.

Test code:

#include <algorithm>

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x):val(x),next(NULL){}
}; 

class Solution {
	ListNode* ReverseList(ListNode* pHead) {

		//If the list is empty or there is only one element in the list
		if (pHead == NULL || pHead->next == NULL)
			return pHead;

		//First, reverse the following list and go to the end node of the list
		ListNode* pReverseNode = ReverseList(pHead->next);
		//Then set the current node as the subsequent node of the subsequent node
		pHead->next->next = pHead;	//The current node is set as the connection direction of the next node
		pHead->next = NULL;	//Experience the emptiness here, very 6!

		return pReverseNode;
	}
};

2. Merge two linked lists that can be sorted

Description: input two monotonically increasing linked lists, and output two synthesized linked lists. Of course, we need the synthesized linked lists to meet the monotone rule.

Idea 1: add an auxiliary table for exchange storage.

Test code:

#include <algorithm>

struct ListNode{
	int val;
	ListNode *next;
	ListNode(int x):val(x),next(NULL){}
};

class Solution {
public:
	ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
		ListNode *p1, *p2, *p3, *pre1;
		pre1 = p1 = pHead1;
		p2 = pHead2;

		while (p1 && p2) {	//p1 and p2 are not empty

			if (p1->val > p2->val) {	//Value, P1 greater than P2
				pre1->next = p2;
				p3 = p2->next;
				p2->next = p1;
				p2 = p3;
			}
			//P1 is less than P2
			pre1 = p1;
			p1 = p1->next;
		}
		if (p2)
			pre1->next = p2;
		return pHead1;
	}
};

Topics: less