zoukankan      html  css  js  c++  java
  • 【LeetCode】02.01. 移除重复节点

    【题目描述】

    编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。

    示例1:

    输入:[1, 2, 3, 3, 2, 1]
    输出:[1, 2, 3]
    示例2:

    输入:[1, 1, 1, 1, 2]
    输出:[1, 2]
    提示:

    链表长度在[0, 20000]范围内。
    链表元素在[0, 20000]范围内。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/remove-duplicate-node-lcci

    【提交代码】

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     struct ListNode *next;
     6  * };
     7  */
     8 
     9 
    10 struct ListNode* removeDuplicateNodes(struct ListNode* head){
    11     struct ListNode *p;
    12     struct ListNode *pre;
    13     struct ListNode *cur;
    14     int val;
    15 
    16     p = head;
    17 
    18     while( p != NULL ) // 遍历整个链表
    19     {
    20         pre = p;
    21         val = pre->val;
    22 
    23         cur = pre->next;
    24         while( cur != NULL ) 
    25         {
    26             if( val == cur->val ) // 如果相同,则删除当前被拿来比较的节点
    27             {
    28                 pre->next = cur->next; 
    29             }
    30             else
    31             {
    32                 pre = pre->next;
    33             }
    34             cur = cur->next;
    35         }
    36 
    37         p = p->next;
    38     }
    39 
    40     return head;
    41 }
  • 相关阅读:
    后端分页但是前端想要把所有数据取出来
    css做多列瀑布流
    css制作骰子
    css球体
    css摩天轮
    css圆圈慢慢放大
    01-布局扩展-自适应两栏布局
    Zigbee 知识简介
    华为OD 嵌入式方向面经
    算法练习题 2 合唱队
  • 原文地址:https://www.cnblogs.com/utank/p/13227357.html
Copyright © 2011-2022 走看看