. Merge Sorted Lists
problem ' s Link
----------------------------------------------------------------------------
Mean:
By merging two non-descending linked lists into a single linked list, the resulting list is still in non-descending order.
Analyse:
The basic operation of the linked list.
Time complexity:o (N)
View Code
/**
* -----------------------------------------------------------------
* Copyright (c) crazyacking. All rights headerved.
* -----------------------------------------------------------------
* author:crazyacking
* date:2016-02-17-16.50
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace STD;
typedef Long Long(LL);
typedef unsigned Long Long(ULL);
Const Double EPS(1e-8);
//Definition for singly-linked list.
struct ListNode
{
int Val;
ListNode *Next;
ListNode(int x): Val(x), Next(NULL){}
};
class Solution
{
Public:
ListNode* mergetwolists(ListNode* L1, ListNode* L2)
{
ListNode *Res=NULL,*Head=NULL;
BOOL IsFirst=true;
while(L1 && L2)
{
if(L1 -Val < L2 -Val)
{
if(IsFirst)
{
Head=New ListNode(L1 -Val);
Res=Head;
IsFirst=false;
}
Else
{
Head -Next=New ListNode(L1 -Val);
Head=Head -Next;
}
L1=L1 -Next;
}
Else
{
if(IsFirst)
{
Head=New ListNode(L2 -Val);
Res=Head;
IsFirst=false;
}
Else
{
Head -Next=New ListNode(L2 -Val);
Head=Head -Next;
}
L2=L2 -Next;
}
}
while(L1)
{
if(IsFirst)
{
Head=New ListNode(L1 -Val);
Res=Head;
IsFirst=false;
}
Else
{
Head -Next=New ListNode(L1 -Val);
Head=Head -Next;
}
L1=L1 -Next;
}
while(L2)
{
if(IsFirst)
{
Head=New ListNode(L2 -Val);
Res=Head;
IsFirst=false;
}
Else
{
Head -Next=New ListNode(L2 -Val);
Head=Head -Next;
}
L2=L2 -Next;
}
return Res;
}
};
int Main()
{
return 0;
}
/*
*/
LeetCode-21. Merge Sorted Lists