算法学习|链表 LeetCode 203.移除链表元素、707.设计链表、206.反转链表

ListNode * p 是指向结构节点的指针,里面只有一个地址。
ListNode * p= new ListNode()是一个结构节点,里面有val和指向下一个节点的结构体指针,而且该节点已经被系统分配内存,在函数体里不会被自动释放。

// 定义单链表节点
struct ListNode {
int val; // 节点上存储的元素
ListNode *next; // 指向下一个节点的指针
ListNode(int x) : val(x), next(NULL) {} // 节点的构造函数
};

1.移除链表元素203

思路

算法学习|链表 LeetCode 203.移除链表元素、707.设计链表、206.反转链表_第1张图片

为什么要使用临时指针?

答:操作完链表之后要返回头结点,直接操作头结点会改变其值,故需要定义一个临时指针指向头结点


1.不使用虚拟头结点

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while(head != NULL && head->val == val) {
            ListNode* tmp = head;
            head = head->next;
        }
        ListNode* cur = head; // 定义一个临时指针来遍历
        while( cur != NULL && cur->next != NULL){
            if( cur->next->val == val){
                ListNode* tmp = cur->next; 
                cur->next = cur->next->next;
                delete tmp; // 释放内存
            }
            else{
                cur = cur -> next;   
            }   
        }
        return head; 
    }
};

2.使用虚拟头结点

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
        dummyHead->next = head; // 将虚拟结点指向头结点
        ListNode* cur = dummyHead; // 定义一个临时指针来遍历
        while( cur->next != NULL){
            if( cur->next->val == val){
                ListNode* tmp = cur->next; 
                cur->next = cur->next->next;
                delete tmp; // 释放内存
            }
            else{
                cur = cur -> next;   
            }   
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
        // 不能直接return head,head有可能被删除,return的一定是链表的头结点
    }
};

2.设计链表707

思路

听Carl老师的讲解吧!


class MyLinkedList {
public:
    // 定义链表节点结构体
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr){}
    };
    // 初始化链表
    MyLinkedList() {
        _dummyHead = new LinkedNode(0); // 定义虚拟头结点
        _size = 0;
    }
    // 获取到第index个节点数值,如果index是非法数值直接返回-1,index是从0开始,第0个结点就是头结点
    int get(int index) {
        if (index > (_size - 1) || index < 0) {
            return -1;
        }
        LinkedNode* cur =_dummyHead->next;
        while (index--) {
            cur = cur->next;
        }
        return cur->val;
    }
    // 在链表最前面插入一个结点,插入完成后,新插入的结点为链表的新头结点
    void addAtHead(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        newNode->next = _dummyHead->next;
        _dummyHead->next = newNode;
        _size++;
    }
    // 在链表最后面添加一个结点
    void addAtTail(int val) {
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur =_dummyHead;
        while(cur->next != nullptr) {
            cur = cur->next;
        }
        cur->next = newNode;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > _size) return;
        if(index < 0) index = 0;
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur =_dummyHead;
        while(cur->next != nullptr) {
            cur = cur->next;
        }
        newNode->next = cur->next;
        cur->next = newNode;
        _size++;
    }
    
    void deleteAtIndex(int index) {
        if(index >= _size || index < 0) {
            return;
        }
        LinkedNode* cur =_dummyHead;
        while(index--) {
            cur = cur->next;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;
        _size--;
    }
    void printLinkedList() {
        LinkedNode* cur= _dummyHead;
        while(cur->next != nullptr){
            cout << cur->next->val << " ";
            cur = cur->next;
        }
        cout << endl;
    }
 private:
      int _size;
      LinkedNode* _dummyHead;
};

3.反转链表206

思路


注意❗

  1. 初始化 :cur= head ;pre=NULL;

  1. 遍历结束:cur指向空的时候循环结束

  1. 使用临时指针保存下一个cur的位置(temp=cur->next;),并改变方向(cur->next=pre;)

  1. 移动pre和cur的指针:先移动pre 再移动cur。(先移动cur,cur的值已经修改,pre无法移动到当初cur的位置)

  1. pre指向新列表的头结点,所以return pre;


// 双指针法
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* temp;
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur) {
            temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
// 递归法
class Solution {
public:
    ListNode* reverse(ListNode* pre, ListNode* cur){
        if(cur == NULL)  return pre;
        ListNode* temp = cur->next;
        cur->next = pre;
        // pre = cur;
        // cur = temp;
        return reverse(cur,temp);
    }
    ListNode* reverseList(ListNode* head) {
        //和双指针法初始化一样
        // ListNode* cur = head;
        // ListNode* pre = NULL;
        return reverse(NULL, head);
    }
};

之前学的真的是一言难尽,今天的虽然还没完全整明白,但是收获好多,感谢Carl老师!明天继续!

如果文章内容出现错误,还请大家多多指正!

你可能感兴趣的:(#,算法学习,算法,c++,leetcode,学习方法)