LeetCode Online Judge 题目C# 练习 - Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

 1         public static ListNode MergeTwoSortedLists(ListNode A, ListNode B)

 2         {

 3             if (A == null)

 4                 return B;

 5             if (B == null)

 6                 return A;

 7 

 8             ListNode ret;

 9             ListNode curr;

10 

11             if (A.val <= B.val)

12             {

13                 ret = A;

14                 A = A.next;

15             }

16             else

17             {

18                 ret = B;

19                 B = B.next;

20             }

21 

22             curr = ret;

23 

24             while (A != null || B != null)

25             {

26                 if (A == null)

27                 {

28                     curr.next = B;

29                     B = B.next;

30                     curr = curr.next;

31                     continue;

32                 }

33                 if (B == null)

34                 {

35                     curr.next = A;

36                     A = A.next;

37                     curr = curr.next;

38                     continue;

39                 }

40                 if (A.val <= B.val)

41                 {

42                     curr.next = A;

43                     A = A.next;

44                     curr = curr.next;

45                     continue;

46                 }

47                 else

48                 {

49                     curr.next = B;

50                     B = B.next;

51                     curr = curr.next;

52                     continue;

53                 }

54             }

55 

56             return ret;

57         }

代码分析:

  最最基本的题了,什么都没考。。。BF,O(n).

你可能感兴趣的:(LeetCode)