开发平台(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题