ListNode节点的定义
template<class T>
struct ListNode {
T val;
ListNode* next;
ListNode(T val) {
this->val = val;
this->next = nullptr;
}
};
链表的几个简单的题目
/*从文件中读取链表数据*/
template<class T>
ListNode<T>* readLinkedList(const char* fileName) {
ListNode<T>* list = new ListNode<T>(NULL);
ListNode<T>* dummyList = list;
T tempVal = NULL;
std::ifstream fin(fileName);
if (fin.is_open()) {
char tempChar;
while (fin >> tempChar && tempChar != ']') {
fin >> tempVal;
list->next = new ListNode<T>(tempVal);
list = list->next;
}
fin.close();
}
tempVal = NULL;
return dummyList->next;
}
/*合并两个非递减链表为非递增链表*/
// 合并到l1上
template<class T>
ListNode<T>* mergeTwoSortedList(ListNode<T>* l1, ListNode<T>* l2) {
if (l1 == nu