• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Remove Duplicates from Sorted List II

忘了边界条件,少数次过

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *deleteDuplicates(ListNode *head) {
12         // Start typing your C/C++ solution below
13         // DO NOT write int main() function
14         if (!head) return NULL;
15         ListNode *pre = new ListNode(head->val-1);
16         pre->next = head;
17         ListNode *cur = pre;
18         while (cur) {
19             ListNode *p = cur->next;
20             bool dup = false;
21             while (p && p->next && p->val == p->next->val) {
22                 dup = true;
23                 p->next = p->next->next;
24             }
25             if (dup) cur->next = p->next;
26             else cur = cur->next;
27         }
28         return pre->next;
29     }
30 };

 C#

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public int val;
 5  *     public ListNode next;
 6  *     public ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode DeleteDuplicates(ListNode head) {
11         if (head == null) return null;
12         ListNode pre = new ListNode(head.val-1);
13         pre.next = head;
14         ListNode cur = pre;
15         while (cur != null) {
16             ListNode p = cur.next;
17             bool dup = false;
18             while (p != null && p.next != null && p.val == p.next.val) {
19                 dup = true;
20                 p.next = p.next.next;
21             }
22             if (dup) cur.next = p.next;
23             else cur = cur.next;
24         }
25         return pre.next;
26     }
27 }
View Code

 

posted @ 2013-04-20 06:11  ying_vincent  阅读(180)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3