gha890826

LeetCode-02

Nov 1st, 2019 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. // leetcode-002.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <stdlib.h> /* 亂數相關函數 */
  7. #include <time.h>
  8. using namespace std;
  9.  
  10.  
  11.  struct ListNode {
  12.      int val;
  13.      ListNode *next;
  14.      ListNode(int x) : val(x), next(NULL) {}
  15.  };
  16.  
  17.  void print(ListNode* pos)
  18.  {
  19.      //pos = pos->next;
  20.      while (pos!= NULL)
  21.      {
  22.          cout << pos->val << " -> ";
  23.          pos = pos->next;
  24.      }
  25.      cout << endl;
  26.      return;
  27.  }
  28.  
  29. class Solution {
  30. public:
  31.     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  32.         ListNode *l1pos = l1,*l2pos=l2;
  33.         ListNode *head = new ListNode(0);
  34.         ListNode *nowpos = head;
  35.         while (l1pos != NULL || l2pos != NULL)
  36.         {
  37.             if (l1pos == NULL)
  38.             {
  39.                 nowpos->next = new ListNode(l2pos->val);
  40.                 l2pos = l2pos->next;
  41.                 nowpos = nowpos->next;
  42.             }
  43.             else if (l2pos == NULL)
  44.             {
  45.                 nowpos->next = new ListNode(l1pos->val);
  46.                 l1pos = l1pos->next;
  47.                 nowpos = nowpos->next;
  48.             }
  49.             else
  50.             {
  51.                 nowpos->next = new ListNode(l1pos->val + l2pos->val);
  52.                 l1pos = l1pos->next;
  53.                 l2pos = l2pos->next;
  54.                 nowpos = nowpos->next;
  55.             }
  56.         }
  57.         nowpos = head;
  58.         while (nowpos != NULL)
  59.         {
  60.             if (nowpos->val >= 10)
  61.             {
  62.                 if (nowpos->next != NULL)
  63.                 {
  64.                     nowpos->next->val += nowpos->val / 10;
  65.                 }
  66.                 else
  67.                 {
  68.                     nowpos->next = new ListNode(nowpos->val / 10);
  69.                 }
  70.             }
  71.             nowpos->val %= 10;
  72.             nowpos = nowpos->next;
  73.         }
  74.         return head->next;
  75.     }
  76. };
  77.  
  78. void make(ListNode *test)
  79. {
  80.     srand(time(NULL));
  81.     for (int i = 0; i < (rand() / (RAND_MAX + 1.0) * 10)+5; i++)
  82.     {
  83.         int x = rand() / (RAND_MAX + 1.0) * 10;
  84.         test->next = new ListNode(x);
  85.         cout << test<<' ' << x << ' ' << test->next << endl;
  86.         test = test->next;
  87.     }
  88.     cout << endl;
  89. }
  90.  
  91. int main()
  92. {
  93.     ListNode *test1 = new ListNode(0), *test2 = new ListNode(0);
  94.     make(test1);
  95.     make(test2);
  96.     system("PAUSE");
  97.     cout << "test1:\n";
  98.     print(test1);
  99.     cout << "test2:\n";
  100.     print(test2);
  101.     Solution sol;
  102.     cout << "add:\n";
  103.     print(sol.addTwoNumbers(test1, test2));
  104.     return 0;
  105. }
Add Comment
Please, Sign In to add comment