LeetCode数据结构基础---2021/8/21

两数相加

两数相加

环形链表 II

环形链表 II

相交链表

剑指 Offer 52. 两个链表的第一个公共节点

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

在这里插入图片描述
分析:
  初始化一个结点作为头结点,指向head,cur指向当前结点,初始为head。如果当前cur(s)和cur->next指向结点的值相同,说明遇到重复值,需要删除;记要删除的值为x,后移cur,保证cur->next->val == x;从s到cur->next都是需要删除的结点;由于cur后移,所以已经删除成功!
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head) {
            return NULL;
        }
        ListNode* ptr = new ListNode(0, head);
        ListNode* cur = ptr;
        while(cur->next && cur->next->next) {
            if(cur->next->val == cur->next->next->val) {
                int x = cur->next->val;
                while(cur->next && cur->next->val == x) {
                    cur->next = cur->next->next;
                }
            }else {
                cur = cur->next;
            }
        }
        return ptr->next;
    }
};

两两交换链表中的节点

两两交换链表中的节点

设计链表

在这里插入图片描述
分析:
  带有头尾指针的单链表:

  1. 初始化:初始化一个头结点p,然后令head和tail都为p
  2. 头部添加:添加后如果len == 1,说明添加前还是空链表,要令tail = head->next。
  3. 尾部添加:添加后如果len == 1,要令head->next = tail
  4. 删除:如果删除前len == 1,删除后要令head->next = tail->next = NULL;如果删除的是最后一个结点,删除后要对tail重新赋值
    代码:
class MyLinkedList {
private:
    struct node {
        int val;
        node* next;
        node(int x):val(x), next(NULL){}
    };
    node* p = new node(0);  //头结点
    node* head = p;
    node* tail = p;
    int len = 0;  //链表长度
public:
    /** Initialize your data structure here. */
    MyLinkedList() {

    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    int get(int index) {
        if(index >= len) {
            return -1;
        }
        node* ptr = head->next;
        for(int i = 0; i < index; i++) {
            ptr = ptr->next;
        }
        return ptr->val;
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    void addAtHead(int val) {
        node* ans = new node(val);
        ans->next = head->next;
        head->next = ans;
        if(len == 0) {
            tail = ans;
        }
        len++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    void addAtTail(int val) {
        node* ans = new node(val);
        tail->next = ans;
        tail = ans;
        len++;
        if(head->next == NULL) {
            head->next = tail;
        }
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    void addAtIndex(int index, int val) {
        if(index > len) {
            return;
        }
        if(index <= 0) {
            addAtHead(val);
            return;
        }
        if(index == len) {
            addAtTail(val);
            return;
        }
        node* ptr = head->next;
        for(int i = 0; i < index - 1; i++) {
            ptr = ptr->next;
        }
        node* ans = new node(val);
        ans->next = ptr->next;
        ptr->next = ans;
        len++;
    }
    
    void print() {
        node* ptr = head->next;
        if(ptr == NULL) {
            cout << "empty" << endl;
            return;
        }
        while(ptr) {
            cout << ptr->val << " --> ";
            ptr = ptr->next;
        }
        cout << endl;
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    void deleteAtIndex(int index) {
        node* t = head->next;
        int cnt = 0;
        if(index < 0 || index >= len) {
            return;
        }
        if(index == 0) {
            head->next = head->next->next;
            if(len == 1) {
                tail = head;
            }
            len--;
            return;
        }
        node* ptr = head->next;
        for(int i = 0; i < index - 1; i++) {
            ptr = ptr->next;
        }
        node* q = ptr->next;
        ptr->next = q->next;
        if(index == len - 1) {
            tail = ptr;
        }
        len--;
    }
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值