日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【11_83】Remove Duplicates from Sorted List

發布時間:2024/6/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【11_83】Remove Duplicates from Sorted List 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這道題本質上不難,難的是細節處理,容易出錯。

第一遍寫的代碼越改越大,越臃腫,此時,不如推倒重寫,果然,第二次一遍過。

?

Remove Duplicates from Sorted List

My Submissions Question Total Accepted:?90731?Total Submissions:?255705?Difficulty:?Easy

?

Given a sorted linked list, delete all duplicates such that each element appear only?once.

For example,
Given?1->1->2, return?1->2.
Given?1->1->2->3->3, return?1->2->3.

?

下面是Discuss里面的好代碼:只有三行!!!

Java寫法

1 public ListNode deleteDuplicates(ListNode head) { 2 if(head == null || head.next == null)return head; 3 head.next = deleteDuplicates(head.next); 4 return head.val == head.next.val ? head.next : head; 5 } View Code

?

另一個好代碼,和我的思想差不多,不過更簡潔:

1 public class Solution { 2 public ListNode deleteDuplicates(ListNode head) { 3 if (head == null) return head; 4 5 ListNode cur = head; 6 while(cur.next != null) { 7 if (cur.val == cur.next.val) { 8 cur.next = cur.next.next; 9 } 10 else cur = cur.next; 11 } 12 return head; 13 } 14 } View Code

?

然后是我自己寫的: C語言 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* deleteDuplicates(struct ListNode* head) { 9 if (head == NULL) 10 return NULL; 11 if (head->next == NULL) 12 return head; 13 14 struct ListNode* p = head; 15 struct ListNode* q = p->next; 16 while(p->next != NULL) { 17 if(p->val == p->next->val) { 18 p->next = p->next->next; 19 } 20 else if(p->next != NULL) 21 p = p->next; 22 } 23 24 return head; 25 }

?

轉載于:https://www.cnblogs.com/QingHuan/p/5051388.html

總結

以上是生活随笔為你收集整理的【11_83】Remove Duplicates from Sorted List的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。