代码随想录1.23

文章目录

    • 链表基础
    • 203.移除链表元素
    • 707.设计链表
    • ==206 反转链表==

链表基础

链表定义:

struct Listnode{
	int val;//链表节点数值
	Listnode *next;//下个节点的指针
}

//使用,定义头节点就行
Listnode *name = new Listnode();
head->val=5;

203.移除链表元素

设置一个虚拟头节点。需要定义了两个新链表,一个是用于存储虚拟头节点的链表,一个用于遍历。

/**
 * 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* removeElements(ListNode* head, int val) {
        ListNode *dummyhead = new ListNode(0); 
        dummyhead->next = head;
        ListNode *cur = dummyhead;
        while(cur->next != nullptr){
            if(cur->next->val == val){
                cur->next = cur->next->next; //执行删除,不用再cur=cur->next!!
            }
            else{
                cur = cur->next;
            }
        }
        return dummyhead->next;
    }
};

707.设计链表

c++中private和public区别:private只能在当前自己这个类中访问,public类外也可以访问,private定义的变量,在同一个类下的public中的函数也可以识别出

  1. 首先在private中创建一个虚拟头节点的链表,以及定义长度,之后就可以在public中的所有函数中都能用这个链表并对其操作
  2. 在public中需要定义Listnode的结构体
  3. 第一个函数实现的是将链表初始化,需要把val和长度都初始赋值为0
  4. 第二个函数是获取链表中下标为 index 的节点的值.注意index无效时候的处理;以及注意定义的游标,初始应该赋值虚拟头节点链表的next!!对于index某个位置的处理,直接–,不需要用一个计数器来看是否到达index位置
  5. 在首尾部和中间某位置添加节点,都可以线定义一个节点的链表(val就是添加节点的值)
class MyLinkedList {
public:
    struct Listnode{
        int val;
        Listnode* next;
        Listnode(int val):val(val), next(nullptr){}
    };
    
    MyLinkedList() {
        dummyhead = new Listnode(0);
        len = 0;
    }
    
    int get(int index) {
        //下面三行注意
        if (index > (len - 1) || index < 0) {
            return -1;
        }
        Listnode * cur = dummyhead->next;//注意是dummy->next不是dummy,从第一个实际节点开始
        int cnt = 0;
        while(index--){
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        Listnode* newnode = new Listnode(val);
        newnode->next = dummyhead->next;
        dummyhead->next = newnode;
        len++;
    }
    
    void addAtTail(int val) {
        Listnode* newnode = new Listnode(val);
        Listnode * cur = dummyhead;
        while(cur->next!=nullptr){
            cur = cur->next;
        }
        cur->next = newnode;
        len++;
    }
    
    void addAtIndex(int index, int val) {
        Listnode *newnode = new Listnode(val);
        Listnode *cur = dummyhead;
        while(index--){
            cur = cur->next;
        }
        newnode->next = cur->next;
        cur->next = newnode;
        len++;
    }
    
    void deleteAtIndex(int index) {
        Listnode *cur = dummyhead;
        if(index>len || index<0) return;
        else{
            while(index--){
                cur=cur->next;
            }
            cur->next = cur->next->next;
        }
        
    }
private:
    Listnode *dummyhead;
    int 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);
 */

206 反转链表

类似于交换ab的值,需要新建一个temp变量来存储

/**
 * 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* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        ListNode* temp;
        while(cur!=nullptr){
            temp=cur->next;
            cur->next=pre;//实现反转
            pre = cur;
            cur = temp;//交换值
        }
        return pre;
    }
};

你可能感兴趣的:(代码随想录跟练记录,c++,力扣,算法,数据结构,链表,开发语言)