0
点赞
收藏
分享

微信扫一扫

力扣第十九天

圣杰 2022-02-04 阅读 39

文章目录

problem Ⅰ

24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)

Example 1:在这里插入图片描述

Example 2:

Example 3:

my solution 1 loop

/**
 * 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* swapPairs(ListNode* head) {
        if(!head || !head->next)return head;
        ListNode *prev = new ListNode();
        prev->next = head;
        ListNode *curr = head, *next = head->next;
        head = prev;
        while(next){
            curr->next = next->next;
            next->next = curr;
            prev->next = next;
            if(!curr->next){//after swap, curr back a little, so use curr to check if continue loop
                return head->next;// even length linkList will be returned here
            }else{
                prev = curr;
                curr = curr->next;
                next = curr->next;
            }
        }
        return head->next;// odd length linkList will be returned here
    }
};

my solution 2 concise solution 1


class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(!head || !head->next)return head;
        ListNode *prev = new ListNode(), *first, *second;
        prev->next = head;
        head = prev;
        while(head->next && head->next->next){
            first = head->next;
            second = head->next->next;
            first->next = second->next;
            second->next = first;
            head->next = second;
            head = head->next->next;
        }
        return prev->next;
    }
};


my problem 3 recurisive (faster, less space)


class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(!head || !head->next)return head;
        ListNode *tmp = head->next;
        head->next = swapPairs(head->next->next);
        tmp->next = head;
        return tmp;
    }
};

problem ⅡRotten problem

707. Design Linked List
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int 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 addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

Example 1:

Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]

Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2);    // linked list becomes 1->2->3
myLinkedList.get(1);              // return 2
myLinkedList.deleteAtIndex(1);    // now the linked list is 1->3
myLinkedList.get(1);              // return 3

my solution

struct Node {
    Node(int value, Node* next = nullptr) : value(value), next(next) {}
    int value;
	Node* next;
};

class MyLinkedList {
public:
    MyLinkedList() = default;
    MyLinkedList(const MyLinkedList&) = delete;
    MyLinkedList(MyLinkedList&&) = delete;

    int get(int index) {
        Node* cur = head;
        if (index >= size || index < 0) return -1;
        while (index--)
            cur = cur->next;

        return cur->value;
    }
    
    void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    void addAtTail(int val) {
        addAtIndex(size, val);
    }
    
    void addAtIndex(int index, int val) {
        if (index > size || index < 0) return;
        Node** pcur = &head;
        while (index--)
            pcur = &((*pcur)->next);

        Node* new_node = new Node(val, *pcur);
        *pcur = new_node;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if (index >= size || index < 0) return;
        Node** pcur = &head;
        while (index--)
            pcur = &((*pcur)->next);

        Node* tmp = *pcur;
        *pcur = (*pcur)->next;
        delete tmp;
        size--;
    }
    
    ~MyLinkedList() {
        while (head) {
            Node* tmp = head;
            head = head->next;
            delete tmp;
        } 
    }

private:
    Node* head = nullptr;
    int size = 0;
};

/**
 * 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);
 */
举报

相关推荐

0 条评论