2018年1月28日 星期日

[2] Add Two Numbers

繼續覺得自己廢= =+
Add Two Numbers
有兩個數字
每個數字用倒過來的linked list表示,
比方說 23二十三就是 先3 -> 2
然後要把這兩個數字相加,
加完再存成一個倒過來的linked list
要注意的是10的進位部份
和兩個list長度不同的時候~~~
舉例來說,
12+ 23 = 35,
輸入是 2->1 跟 3-> 2
輸出是 5->3
輸入是5,5的話, 輸出是 0->1
(我真的需要寫這麼細嗎XD~)

小廢廢我的版本:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* createNode(int val)
{
    struct ListNode *node = malloc(sizeof(struct ListNode));
    node->val = val;
    node->next = NULL;
    return node;
}

void appendQList(struct ListNode **head, struct ListNode *node)
{
    if (*head == NULL)
    {
        (*head) = node;
        return;
    }
    struct ListNode *tmp = (*head);
    for (tmp = *head;tmp->next!=NULL;tmp=tmp->next)
        ;
    tmp->next = node;
    return;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *result=NULL,*p1,*p2;
    bool tenflag = 0;
    for(p1=l1,p2=l2;(p1 != NULL || p2 != NULL);)
    {
        int v1 = (p1== NULL)? 0:p1->val;
        int v2 = (p2== NULL)? 0:p2->val;    
        int sum = v1+v2 + tenflag;
        tenflag = (sum < 10) ? 0:1;
        sum = (tenflag)? sum - 10: sum;
        appendQList(&result,createNode(sum));

        if(p1!=NULL)
            p1=(p1->next ==NULL)? NULL:(p1->next);
        if(p2!=NULL)
            p2=(p2->next ==NULL)? NULL:(p2->next);
     
    }
 
    if (tenflag)
    {
        appendQList(&result,createNode(tenflag));
        tenflag = 0;
    }

    return result;
}

寫完看了一下別人的, 覺得進位的地方似乎有更好的寫法
但是懶得想了 XD 先這樣吧QQ 我要前進下一題屋屋.
一開始在appendList的地方卡好久
完全就是call by address和call by value沒學好QQ
順便附上兩年前複習的時候的內心吶喊吧哈哈哈哈哈
====================================================
1.剛剛才發現,原來call by addresscall by reference 是不一樣的。
2.我一直以為只有call by value call by reference兩種 囧
3.我根本不知道C沒有call by referenceC++才有。 (!)
4.拿出C語言課本想證明我的記憶沒有錯,發現相關章節的標題旁邊,我本人的字跡寫著:「C沒有call by reference」(WTF XD)

5.有一種根本沒寫過C++fu,我吃屎好了我面試個屁 T—T(大哭奔入雨中)


[20240608 update]
啊...............真是全新的世界啊XD 原來我的linked list 還是一直沒有學好 XD

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertNode(int val)
{
struct ListNode *ptr = calloc (1, sizeof(struct ListNode));
ptr->val = val;
ptr->next = NULL;
return ptr;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *p1 = l1;
struct ListNode *p2 = l2;
struct ListNode head;
head.next = NULL;
struct ListNode *tail = &head;
int overflow=0;
while (p1 != NULL || p2 != NULL || overflow)
{
int v1=0, v2=0, add=0;
if (p1!=NULL)
{
v1 = p1->val;
p1 = p1->next;
}
if (p2!= NULL)
{
v2 = p2->val;
p2 = p2->next;
}

add = v1+v2 + overflow;
if (add >= 10)
{
overflow = 1;
add-=10;
}
else
overflow = 0;

tail->next = insertNode(add);
tail = tail->next;
}
return head.next;
}
沒什麼差別的更新版 (?)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertNode(int val)
{
struct ListNode *ptr = calloc (1, sizeof(struct ListNode));
ptr->val = val;
ptr->next = NULL;
return ptr;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode *p1 = l1;
struct ListNode *p2 = l2;
struct ListNode *head = NULL;
struct ListNode *tail = NULL;

int overflow=0;
while (p1 != NULL || p2 != NULL || overflow)
{
int v1=0, v2=0, add=0;
if (p1!=NULL)
{
v1 = p1->val;
p1 = p1->next;
}
if (p2!= NULL)
{
v2 = p2->val;
p2 = p2->next;
}

add = v1+v2 + overflow;
if (add >= 10)
{
overflow = 1;
add-=10;
}
else
overflow = 0;

if (head == NULL)
{
head = insertNode(add);
tail = head;
}
else
{
tail->next = insertNode(add);
tail = tail->next;
}
}
return head;
}

[20250821]
再次更新?! 嗯.....終於把多出來的insert node 塞進去了(嗎)
好笨QQ 感覺要來把dummy pointer 學起來了/_\

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    if (l1==NULL)
        return l2;
    if (l2==NULL)
        return l1;
    struct ListNode *head = NULL;
    struct ListNode *tail = NULL;
    int plus1= 0;
    while (l1 != NULL || l2 !=NULL || plus1>0)
    {
        int sum = plus1;
        if (l1 == NULL && l2 == NULL)
            sum = plus1;
        else if (l1 == NULL)
            sum += l2->val;
        else if (l2 == NULL)
            sum += l1->val;
        else 
            sum += (l1->val + l2->val);

        if (sum > 9)
        {
            plus1 = 1;
            sum -= 10;
        }
        else
            plus1 = 0;
        struct ListNode *new = calloc (1, sizeof (struct ListNode)); 
        if (head == NULL)
            head = new;
        else
            tail->next = new;
        new->val = sum;
        new->next = NULL;
        tail = new;
        if (l1 != NULL)
            l1 = l1->next;
        if (l2 != NULL)
            l2= l2->next;
    }

    return head;
}

沒有留言:

張貼留言