刷题笔记-链表
完整链表操作模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <algorithm> #include <iostream> #include <vector> using namespace std;
class MyLinkedList { public: 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) {} }; MyLinkedList() { _size = 0; _dummyHead = new ListNode(0); } int get(int index) { if (index > _size - 1 || index < 0) { return -1; } ListNode* cur = _dummyHead; while (index--) { cur = cur->next; } return cur->next->val; } void addAtHead(int val) { ListNode* newNode = new ListNode(val); newNode->next = _dummyHead->next; _dummyHead->next = newNode; _size++; } void addAtTail(int val) { ListNode* newNode = new ListNode(val); ListNode* 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) { addAtHead(val); return; } if (index == _size) { addAtTail(val); return; } ListNode* newNode = new ListNode(val); ListNode* cur = _dummyHead; while (index--) { cur = cur->next; } newNode->next = cur->next; cur->next = newNode; _size++; } void deleteAtIndex(int index) { if (index < 0 || index > _size - 1) { cout << "index out of range!" << endl; return; } ListNode* cur = _dummyHead; while (index--) { cur = cur->next; } ListNode* temp = cur->next; cur->next = cur->next->next; delete temp; _size--; } void printLinkedList() { ListNode* cur = _dummyHead; while (cur->next != nullptr) { cout << cur->next->val << " "; cur = cur->next; } cout << endl; } void reverseList() { ListNode* temp; ListNode* cur = _dummyHead->next; ListNode* pre = nullptr; while (cur) { temp = cur->next; cur->next = pre; pre = cur; cur = temp; } _dummyHead->next = pre; }
void removeNthFromEnd(int n) { ListNode* slow = _dummyHead; ListNode* fast = _dummyHead; while (n-- && fast != nullptr) { fast = fast->next; } if (fast == nullptr) { return; } while (fast->next != nullptr) { slow = slow->next; fast = fast->next; } ListNode* tem = slow->next; slow->next = slow->next->next; delete tem; } void swap() { ListNode* cur = _dummyHead; while (cur->next != nullptr && cur->next->next != nullptr) { ListNode* tmp = cur->next; ListNode* tmp1 = cur->next->next->next;
cur->next = cur->next->next; cur->next->next = tmp; cur->next->next->next = tmp1;
cur = cur->next->next; } }
private: int _size; ListNode* _dummyHead; };
int main() { MyLinkedList linkedList; linkedList.addAtHead(1); linkedList.addAtHead(2); linkedList.addAtHead(3); linkedList.addAtTail(3); linkedList.printLinkedList(); }
|
https://leetcode-cn.com/problems/linked-list-cycle-ii/
反转链表
1 2 3 4 5 6 7 8 9 10 11
| public void reverseList(ListNode head) { ListNode tmp = null; ListNode pre = null; ListNode cur = head; while (cur != null) { tmp = cur.next; cur.next = pre; pre = cur; cur = tmp; } }
|