-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{55D7F9A4-14A2-40EE-9EAB-2A2D03D19EBE}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>My25_MergeSortedLists</RootNamespace> | ||
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader> | ||
</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClCompile Include="MergeSortedLists.cpp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Utilities\Utilities.vcxproj"> | ||
<Project>{732da13a-f8bb-4cef-b96d-61de60bdb6a7}</Project> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup> | ||
<Filter Include="Source Files"> | ||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | ||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | ||
</Filter> | ||
<Filter Include="Header Files"> | ||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | ||
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> | ||
</Filter> | ||
<Filter Include="Resource Files"> | ||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | ||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | ||
</Filter> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="MergeSortedLists.cpp"> | ||
<Filter>Source Files</Filter> | ||
</ClCompile> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
//================================================================== | ||
// 《剑指Offer——名企面试官精讲典型编程题》代码 | ||
// 著作权所有者:何海涛 | ||
//================================================================== | ||
|
||
// 面试题24:合并两个排序的链表 | ||
// 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按 | ||
// 照递增排序的。例如输入图3.11中的链表1和链表2,则合并之后的升序链表如链 | ||
// 表3所示。 | ||
|
||
#include "cstdio" | ||
#include "..\Utilities\List.h" | ||
|
||
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) | ||
{ | ||
if(pHead1 == nullptr) | ||
return pHead2; | ||
else if(pHead2 == nullptr) | ||
return pHead1; | ||
|
||
ListNode* pMergedHead = nullptr; | ||
|
||
if(pHead1->m_nValue < pHead2->m_nValue) | ||
{ | ||
pMergedHead = pHead1; | ||
pMergedHead->m_pNext = Merge(pHead1->m_pNext, pHead2); | ||
} | ||
else | ||
{ | ||
pMergedHead = pHead2; | ||
pMergedHead->m_pNext = Merge(pHead1, pHead2->m_pNext); | ||
} | ||
|
||
return pMergedHead; | ||
} | ||
|
||
// ====================测试代码==================== | ||
ListNode* Test(char* testName, ListNode* pHead1, ListNode* pHead2) | ||
{ | ||
if(testName != nullptr) | ||
printf("%s begins:\n", testName); | ||
|
||
printf("The first list is:\n"); | ||
PrintList(pHead1); | ||
|
||
printf("The second list is:\n"); | ||
PrintList(pHead2); | ||
|
||
printf("The merged list is:\n"); | ||
ListNode* pMergedHead = Merge(pHead1, pHead2); | ||
PrintList(pMergedHead); | ||
|
||
printf("\n\n"); | ||
|
||
return pMergedHead; | ||
} | ||
|
||
// list1: 1->3->5 | ||
// list2: 2->4->6 | ||
void Test1() | ||
{ | ||
ListNode* pNode1 = CreateListNode(1); | ||
ListNode* pNode3 = CreateListNode(3); | ||
ListNode* pNode5 = CreateListNode(5); | ||
|
||
ConnectListNodes(pNode1, pNode3); | ||
ConnectListNodes(pNode3, pNode5); | ||
|
||
ListNode* pNode2 = CreateListNode(2); | ||
ListNode* pNode4 = CreateListNode(4); | ||
ListNode* pNode6 = CreateListNode(6); | ||
|
||
ConnectListNodes(pNode2, pNode4); | ||
ConnectListNodes(pNode4, pNode6); | ||
|
||
ListNode* pMergedHead = Test("Test1", pNode1, pNode2); | ||
|
||
DestroyList(pMergedHead); | ||
} | ||
|
||
// 两个链表中有重复的数字 | ||
// list1: 1->3->5 | ||
// list2: 1->3->5 | ||
void Test2() | ||
{ | ||
ListNode* pNode1 = CreateListNode(1); | ||
ListNode* pNode3 = CreateListNode(3); | ||
ListNode* pNode5 = CreateListNode(5); | ||
|
||
ConnectListNodes(pNode1, pNode3); | ||
ConnectListNodes(pNode3, pNode5); | ||
|
||
ListNode* pNode2 = CreateListNode(1); | ||
ListNode* pNode4 = CreateListNode(3); | ||
ListNode* pNode6 = CreateListNode(5); | ||
|
||
ConnectListNodes(pNode2, pNode4); | ||
ConnectListNodes(pNode4, pNode6); | ||
|
||
ListNode* pMergedHead = Test("Test2", pNode1, pNode2); | ||
|
||
DestroyList(pMergedHead); | ||
} | ||
|
||
// 两个链表都只有一个数字 | ||
// list1: 1 | ||
// list2: 2 | ||
void Test3() | ||
{ | ||
ListNode* pNode1 = CreateListNode(1); | ||
ListNode* pNode2 = CreateListNode(2); | ||
|
||
ListNode* pMergedHead = Test("Test3", pNode1, pNode2); | ||
|
||
DestroyList(pMergedHead); | ||
} | ||
|
||
// 一个链表为空链表 | ||
// list1: 1->3->5 | ||
// list2: 空链表 | ||
void Test4() | ||
{ | ||
ListNode* pNode1 = CreateListNode(1); | ||
ListNode* pNode3 = CreateListNode(3); | ||
ListNode* pNode5 = CreateListNode(5); | ||
|
||
ConnectListNodes(pNode1, pNode3); | ||
ConnectListNodes(pNode3, pNode5); | ||
|
||
ListNode* pMergedHead = Test("Test4", pNode1, nullptr); | ||
|
||
DestroyList(pMergedHead); | ||
} | ||
|
||
// 两个链表都为空链表 | ||
// list1: 空链表 | ||
// list2: 空链表 | ||
void Test5() | ||
{ | ||
ListNode* pMergedHead = Test("Test5", nullptr, nullptr); | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
Test1(); | ||
Test2(); | ||
Test3(); | ||
Test4(); | ||
Test5(); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters