数据结构期末考试题目---笔记(SYSU)

94 篇文章 4 订阅
40 篇文章 0 订阅

也不知道为什么考试的时候好像脑子抽了一样,这么简单的一个题目居然慌了神没有写
结果 90分变成了80分???
我的国奖梦啊!!!

当然这也说明我对于指针没有想象中的那么熟悉,导致了我在慌乱的情况下就没有了那么高的水准,这点要检讨。
希望以后看这个博客的其他同学们也要引以为戒。

题目意思:
将一个链表的连续的重复数字给删掉变成一个

就是 1 -> 2 -> 2 -> 3 -> NULL
变成 1 -> 2 -> 3 -> NULL

#include <iostream>
using namespace std;

struct Node{
    int val;
    Node* next;
    Node(int v = 0){
        val = v; next = NULL; 
    } 
};


void PRINT(Node* head) {
    Node *cur = head;
    while (cur != NULL) {
        cout << cur->val<<" --> ";
        cur = cur->next;
    }
    cout << "NULL\n";
} 

Node* DELETESAME( Node* head ) {
    int last = 0;
    Node *cur = head, *pre, *s;
    while (cur != NULL) {
        if (cur == head || cur->val != last) {
            last = cur -> val;
            pre = cur;
            cur = cur->next;
        } else {
            // delete cur; last 不更新 pre 也不更新 
            s = cur;
            pre -> next = cur->next;
            cur = cur -> next;
            delete s;
            s = NULL; 
        }
    } 
    return head;
} 

int main(){
    Node *head = new Node;
    head->val = 1;
    Node *cur = head;
    for (int i = 2; i < 5; ++i) {
        for (int j = 1; j <= 3; ++j) {
            cur->next = new Node(i);
            cur = cur->next;
        }
    }
    PRINT(head); 
    head = DELETESAME(head);
    PRINT(head);
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥宅_Sean

公众号“肥宅Sean”欢迎关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值