Merge ordered linked list

Source: Internet
Author: User

  1. Using namespace std;
  2. struct ListNode
  3. {
  4. int m_data;
  5. listnode* M_pnext;
  6. ListNode (int value,listnode* next = NULL): m_data (value), M_pnext (next) {}
  7. };
  8. /*
  9. Two linked lists such as linked list 1:1->3->5->7->9
  10. Linked list 2:2->4->6->8->10
  11. As we merge two arrays, the head node of the list 1 is compared with the head nodes of the linked list 2, if the value of the linked List 1 head node is greater than the value of the linked list 2 header point,
  12. Then the first node of the list 2 is the head node of the consolidated list, then the head nodes of the list 1 continue and the second nodes of the linked List 2 (the head nodes of the remaining list 2)
  13. For comparison, but after a linked list has been traversed, if another linked list has not been traversed, because the linked list is already sorted, so let the merge list
  14. The tail node points to the head nodes that have not traversed the linked list.
  15. As an example:
  16. Linked list 1:1,3,5,23,34;
  17. Linked list 2:2,4,6,8,10;
  18. After traversing the list 3:1,2,3,4,8,10, the list 2 has been traversed, while the loop exits, but the remaining list 1 has 23,34
  19. At this point let the tail node of the list 3 10 link The remaining list of the head node 23 will be able to
  20. */
  21. listnode* MergeList2 (listnode* head1,listnode* head2)
  22. {
  23. if (head1 = = NULL)
  24. {
  25. return head2;
  26. }
  27. Else if (head2 = = NULL)
  28. {
  29. return head1;
  30. }
  31. listnode* mergehead = NULL;
  32. if (Head1->m_data < Head2->m_data)
  33. {
  34. Mergehead = Head1;
  35. Head1 = head1->m_pnext;
  36. }
  37. Else
  38. {
  39. Mergehead = head2;
  40. Head2 = head2->m_pnext;
  41. }
  42. listnode* Tmpnode = Mergehead;
  43. While (head1&&head2)
  44. {
  45. if (Head1->m_data < Head2->m_data)
  46. {
  47. Mergehead->m_pnext = Head1;
  48. Head1 = head1->m_pnext;
  49. }
  50. Else
  51. {
  52. Mergehead->m_pnext = head2;
  53. Head2 = head2->m_pnext;
  54. }
  55. Mergehead = mergehead->m_pnext;
  56. }
  57. if (head1)
  58. {
  59. Mergehead->m_pnext = Head1;
  60. }
  61. if (head2)
  62. {
  63. Mergehead->m_pnext = head2;
  64. }
  65. return tmpnode;
  66. }
  67. int _tmain (int argc, _tchar* argv[])
  68. {
  69. listnode* pHead1 = new ListNode (1);
  70. listnode* pcur = pHead1;
  71. For (int i = 3; i <; i+=2)
  72. {
  73. listnode* Tmpnode = new ListNode (i);
  74. Pcur->m_pnext = Tmpnode;
  75. Pcur = Tmpnode;
  76. }
  77. listnode* pHead2 = new ListNode (2);
  78. Pcur = pHead2;
  79. For (int j = 4; J < j+=2)
  80. {
  81. listnode* Tmpnode = new ListNode (j);
  82. Pcur->m_pnext = Tmpnode;
  83. Pcur = Tmpnode;
  84. }
  85. listnode* head = MergeList2 (phead1,phead2);
  86. While (head)
  87. {
  88. cout<"";
  89. head=head->m_pnext;
  90. }
  91. GetChar ();
  92. return 0;
  93. }</span>

Merging ordered lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.