# info2021-homework2 ###### tags : `資訊科技產業面試` :::warning 英文版只有稍加修飾,與加上一題新的題目 中文版是全新的版本 ::: ### interviewer 自評 1. 比上次能有更多的敘述 2. 能中斷面試者的過程 3. ### interviewee 1. 會對問題的內容有更多的提問,像是node 的資料型別等等 2. 對資料結構的名詞適用不夠確切 3. ### question1 : ```c++= struct ListNode{ int val; ListNode* next; }; class Solution { public: int getDecimalValue(ListNode* head) { int sum = 0 ; ListNode* pointer= head; while(pointer != NULL){ sum = sum *2 ; sum = sum + pointer->val; pointer= pointer->next; } return sum; } }; ``` ### question2 : ```c++= class Solution { public: ListNode* deleteNodes(ListNode* head, int m, int n) { ListNode* pointer=head; int remove = n; int keep = m-1 ; while(pointer != NULL){ if(keep != 0){ pointer = pointer->next; keep--; }else if(remove != 0){ if(pointer->next == NULL) break; pointer->next = pointer->next->next; remove--; }else{ keep = m; remove = n ; } } return head; } }; ``` ### question3: 這題有一個利用 pointer to pointer的寫法 ```c++= ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int sum=0; ListNode *head, *tail_pointer; ListNode* node = new ListNode; // new struct ListNode 跟 new ListNode 一樣 head = node; tail_pointer = node; while(l1 != NULL || l2 !=NULL || sum != 0){ if(l1 != NULL){ sum = sum + l1->val; l1 = l1->next; } if(l2 != NULL){ sum = sum + l2->val; l2 = l2->next; } tail_pointer->val = sum%10; sum = sum/10; if(l1 != NULL || l2 !=NULL || sum != 0){ ListNode* node = new ListNode; tail_pointer->next = node; tail_pointer = tail_pointer->next; } } return head; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up