first commit
This commit is contained in:
33
l33tcode_merge2sortedlists/Cpp/CMakeLists.txt
Normal file
33
l33tcode_merge2sortedlists/Cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# Modify only these if one source file!
|
||||
project(Cppl33tcode_merge2sortedlists)
|
||||
set(CURRENT_PROJECT_CODE_NAME l33tcode_merge2sortedlists)
|
||||
set(FILE_EXT cpp)
|
||||
# End
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# We want all the warnings and as errors enabled
|
||||
if (MSVC)
|
||||
# warning level 4 and all warnings as errors
|
||||
add_compile_options(/W4 /WX)
|
||||
else()
|
||||
# lots of warnings and all warnings as errors
|
||||
add_compile_options(-Wall -Wextra -pedantic -Werror)
|
||||
endif()
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${CURRENT_PROJECT_CODE_NAME}.${FILE_EXT})
|
||||
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_INCLUDES}
|
||||
)
|
||||
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
|
||||
${EXTRA_LIBS}
|
||||
)
|
||||
|
||||
|
||||
111
l33tcode_merge2sortedlists/Cpp/l33tcode_merge2sortedlists.cpp
Normal file
111
l33tcode_merge2sortedlists/Cpp/l33tcode_merge2sortedlists.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
// Merge2SortedLists.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode* next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* struct ListNode {
|
||||
* int val;
|
||||
* ListNode *next;
|
||||
* ListNode() : val(0), next(nullptr) {}
|
||||
* ListNode(int x) : val(x), next(nullptr) {}
|
||||
* ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
|
||||
ListNode* head = nullptr;
|
||||
|
||||
if (l1 && l2 && l1->val < l2->val)
|
||||
{
|
||||
head = l1;
|
||||
l1 = l1->next;
|
||||
head->next = nullptr;
|
||||
}
|
||||
else if (l2)
|
||||
{
|
||||
head = l2;
|
||||
l2 = l2->next;
|
||||
head->next = nullptr;
|
||||
}
|
||||
|
||||
ListNode* newList = head;
|
||||
|
||||
if (l1 && l2)
|
||||
{
|
||||
while (l1 != nullptr && l2 != nullptr)
|
||||
{
|
||||
if (l1->val < l2->val)
|
||||
{
|
||||
newList->next = l1;
|
||||
l1 = l1->next;
|
||||
newList = newList->next;
|
||||
newList->next = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
newList->next = l2;
|
||||
l2 = l2->next;
|
||||
newList = newList->next;
|
||||
newList->next = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (l1 != nullptr)
|
||||
{
|
||||
newList->next = l1;
|
||||
newList = newList->next;
|
||||
newList->next = nullptr;
|
||||
}
|
||||
|
||||
while (l2 != nullptr)
|
||||
{
|
||||
newList->next = l2;
|
||||
newList = newList->next;
|
||||
newList->next = nullptr;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Solution s;
|
||||
s.mergeTwoLists(nullptr, nullptr);
|
||||
|
||||
ListNode *one = new ListNode(4);
|
||||
ListNode* two = new ListNode(2, one);
|
||||
ListNode* three = new ListNode(1, two);
|
||||
|
||||
ListNode* one1 = new ListNode(4);
|
||||
ListNode* two1 = new ListNode(3, one);
|
||||
ListNode* three1 = new ListNode(1, two);
|
||||
|
||||
s.mergeTwoLists(three, three1);
|
||||
|
||||
std::cout << "Hello World!\n";
|
||||
}
|
||||
|
||||
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
|
||||
// Debug program: F5 or Debug > Start Debugging menu
|
||||
|
||||
// Tips for Getting Started:
|
||||
// 1. Use the Solution Explorer window to add/manage files
|
||||
// 2. Use the Team Explorer window to connect to source control
|
||||
// 3. Use the Output window to see build output and other messages
|
||||
// 4. Use the Error List window to view errors
|
||||
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
|
||||
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
|
||||
Reference in New Issue
Block a user