[問題] 針對pointer設pointer變數

看板C_and_CPP (C/C++)作者 (水肅)時間7年前 (2018/06/08 11:05), 編輯推噓3(309)
留言12則, 5人參與, 7年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Unix 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) GCC-6.2.0 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) No 問題(Question): 小弟最近在leetcode上練功 自己有寫出答案,但是跑得不夠快,看一下其他高手寫的 我看到一段程式碼,他是這樣寫 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if (head == NULL || head -> next == NULL){ return head; } *[1;33ListNode* current = head;*[m while (current -> next != NULL){ if (current -> val == current -> next -> val){ current -> next = current -> next -> next; } else{ current = current -> next; } } *[1;33return head;*[m } }; 看一下標色的字,這邊新設一個pointer變數current,assign head進來 但是return是return head 想請問一下,是不是這種設法,就是會讓current的值和head連動? 這種編法術語是什麼呢? 他的好處在哪裡呢?謝謝 餵入的資料(Input): Input: 1->1->2 預期的正確結果(Expected Output): Output: 1->2 錯誤結果(Wrong Output):程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 如上 補充說明(Supplement): leetcode 83題 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 100.8.214.54 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1528427131.A.8E7.html

06/08 11:43, 7年前 , 1F
兩個無關 這樣的好處... 樓下說他知道 XD
06/08 11:43, 1F

06/08 11:48, 7年前 , 2F
我只知道你顏色跑掉了 esc要用Ctrl-U輸入(對嗎?)
06/08 11:48, 2F

06/08 11:57, 7年前 , 3F
沒有連動唷,將重複值的link拔掉後,回傳 list 的開頭
06/08 11:57, 3F

06/08 16:29, 7年前 , 4F
這題目看起來很簡單 你寫不夠快應該是因為你的演算法複
06/08 16:29, 4F

06/08 16:29, 7年前 , 5F
雜度太高 然後這不是什麼特殊技巧 就只是用另一個指標
06/08 16:29, 5F

06/08 16:29, 7年前 , 6F
去操作 list
06/08 16:29, 6F

06/08 17:44, 7年前 , 7F
return的問題在於傳head進去, 操作head以外的東西時沒什
06/08 17:44, 7F

06/08 17:44, 7年前 , 8F
麼問題,而如果改到head的話就需要一個固定管道傳出來,
06/08 17:44, 8F

06/08 17:44, 7年前 , 9F
方法有return head, 傳參數用指標的指標或pass by refere
06/08 17:44, 9F

06/08 17:44, 7年前 , 10F
nce等, 一般list操作最make sense的是return head, 在其
06/08 17:44, 10F

06/08 17:44, 7年前 , 11F
他做法或應用時不得已(return不夠用之類的)再考慮其他做
06/08 17:44, 11F

06/08 17:44, 7年前 , 12F
06/08 17:44, 12F
文章代碼(AID): #1R6V9xZd (C_and_CPP)
文章代碼(AID): #1R6V9xZd (C_and_CPP)