zoukankan      html  css  js  c++  java
  • lintcode112 删除排序链表中的重复元素

    删除排序链表中的重复元素 

     

    给定一个排序链表,删除所有重复的元素每个元素只留下一个。

    样例

    给出 1->1->2->null,返回 1->2->null

    给出 1->1->2->3->3->null,返回 1->2->3->null

     1 class Solution {
     2 public:
     3     /*
     4      * @param head: head is the head of the linked list
     5      * @return: head of linked list
     6      */
     7     ListNode * deleteDuplicates(ListNode * head) {
     8         // write your code here
     9         if (head == NULL) {
    10             return head;
    11         }
    12         ListNode *dummy;
    13         dummy = head;
    14         while (head->next != NULL) {
    15             if (head->val == head->next->val) {
    16                 *head = *(head->next);
    17             } else {
    18                 head = head->next;
    19             }
    20         }
    21         return dummy;
    22     }
    23 };
  • 相关阅读:
    IOTest-InputStream-OutputStream
    JSP
    java链表
    区块链
    MySQL常用命令
    jQuery
    javascript
    Nginx
    Linux
    Hive
  • 原文地址:https://www.cnblogs.com/gousheng/p/7647879.html
Copyright © 2011-2022 走看看