链表的重复删除/反转

来源:互联网 发布:客户数据库管理软件 编辑:程序博客网 时间:2024/06/06 17:09
remove dupliates from sorted list
#include <iostream>#include <windows.h>using namespace std;struct Node{int data;struct Node *next;Node (int x) : data(x), next(nullptr) { }};Node* pra(Node* head){//重复删除if (head == NULL) return head;/*for (Node*pre = head, *cur = head->next; cur; cur =pre->next){if (cur->data == pre->data){pre->next = cur->next;delete cur;}elsepre = cur;}*/Node*pre = head, *cur = head->next;while (cur != NULL){if (cur->data == pre->data){pre->next = cur->next;delete cur;}else{           pre = cur;}cur = pre->next;}return head;}int main(){Node* tail = new Node(-1);Node* ptr = tail;int m;int c; cin >> m;while (m){m--;cin >> c;ptr->next = new Node(c);ptr = ptr ->next;}int x;Node* eail = pra(tail->next);while (eail != NULL){cout << eail->data << endl;eail = eail->next;}return 0;}
#include <iostream>  #include <windows.h>  using namespace std;struct Node{int data;struct Node *next;Node(int x) : data(x), next(nullptr) { }};Node *pra(Node *head){//链表的反转if (head == NULL)return NULL;Node* p = head;Node* newl=NULL;while (p){Node *tmp = p->next;p->next = newl;newl = p;p = tmp;}return newl;}int main(){Node* tail = new Node(-1);Node* ptr = tail;int m;int c;cin >> m;while (m){m--;cin >> c;ptr->next = new Node(c);ptr = ptr->next;}int x;Node* eail = pra(tail->next);while (eail != NULL){cout << eail->data << endl;eail = eail->next;}return 0;}


原创粉丝点击