206
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* dummyHead = new ListNode;
dummyHead->next = nullptr;
ListNode* cur = head;
ListNode* next;
while(cur){
next = cur->next;
cur->next = dummyHead->next;
dummyHead->next = cur;
cur = next;
}
return dummyHead->next;
}
};
这个反而是最好的
void reverseLinkedList(ListNode* head){
ListNode* pre = nullptr;
ListNode* cur = head;
while(cur){
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
}
ListNode* reverseList(ListNode* head) {
ListNode* pre = nullptr;
ListNode* cur = head;
ListNode* next;
while(cur){
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
92.
反转链表2??
为什么错了??不能先把链表断开了!!
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
ListNode* dummyNode = new ListNode(-1);
dummyNode->next = head; //不要以为这个head名字没改,但已经面目全非
ListNode* lNode = head;
ListNode* prev;
ListNode* rNode = head;
for(int i=0; i<left-1; i++){
prev = lNode;
lNode = lNode->next;
}
prev->next = nullptr;
for(int j=0; j< right-1; j++) rNode = rNode->next;
ListNode* next = rNode->next;
rNode->next = nullptr;
reverseLinkedList(lNode);
prev->next = rNode;
lNode->next = next;
return dummyNode->next;
}
void reverseLinkedList(ListNode* head){
ListNode* pre = nullptr;
ListNode* cur = head;
while(cur){
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
}
};
160.??
为什么出现这个错误??
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* alist = reverseList(headA);
ListNode* blist = reverseList(headB);
ListNode* ans;
while(alist && blist && alist->val ==blist->val){
ans = alist;
alist = alist->next;
blist = blist->next;
}
return ans;
}
ListNode* reverseList(ListNode* head){
ListNode* dummyHead = new ListNode;
dummyHead->next = head;
ListNode* pre = nullptr;
ListNode* cur = head;
while(cur){
ListNode* next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return dummyHead->next;
}
};
203.
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyHead = new ListNode;
dummyHead->next = head;
ListNode* cur = head;
ListNode* prev = dummyHead;
while(cur){
if(cur->val == val){
prev->next = cur->next;
cur = prev->next;
}else{
prev = cur;
cur = cur->next;
}
}
return dummyHead->next;
}
};
237.
要删一个节点,却不知道它前一个节点??这个位置copy一份它下一个节点,删它下一个节点。
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
1721.
- 改变节点? 其一是就改变节点的值!!由于不涉及交换,连dummyHead都可以省略了!!
- 其二是交换,显然此处不好交换。L R slow fast这样看起来多清晰!!!
- 因为最后一个点到了,slow,fast仍然要向右移动,所以多出一个。while(fast->next)
错了??这样找效率太低??
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* dummyHead = new ListNode;
dummyHead->next = head;
ListNode* front = head;
ListNode* prev1 = dummyHead;
for(int i=0; i<k-1; i++){
prev1 = prev1->next;
front = front->next;
}
ListNode* end = front;
ListNode* prev2 = dummyHead;
ListNode* rNode = head;
while(end->next){
prev2 = prev2->next;
rNode = rNode->next;
end = end->next;
}
ListNode* rNext = rNode->next;
rNode->next = front->next;
prev1->next = rNode;
prev2->next = front;
front->next = rNext;
return dummyHead->next;
}
};
//错解
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* fast = head;
for(int i=0; i<k-1; i++) fast= fast->next;
ListNode* slow = head;
ListNode* L = fast;
while(fast){ //就是这里错了
slow = slow->next;
fast = fast->next;
}
ListNode* R = slow;
int tmp = L->val;
L->val = R->val;
R->val = tmp;
return head;
}
};
正确:
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* fast = head;
for(int i=0; i<k-1; i++) fast= fast->next;
ListNode* slow = head;
ListNode* L = fast;
while(fast->next){
slow = slow->next;
fast = fast->next;
}
ListNode* R = slow;
int tmp = L->val;
L->val = R->val;
R->val = tmp;
return head;
}
};
1019.
人家用单调栈???这是个什么东西?
class Solution {
public:
vector<int> nextLargerNodes(ListNode* head) {
vector<int> ans;
while(head){
ListNode* cur = head;
int value = cur->val;
while(cur && cur->val <= value){
cur = cur->next;
}
if(cur) head->val = cur->val;
else head->val = 0;
ans.push_back(head->val);
head = head->next;
}
return ans;
}
};
1669
- 这里走几步是我安装案例抠出来的。。。。
class Solution {
public:
ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
ListNode* dummyHead = new ListNode;
dummyHead->next = list1;
ListNode* cur = list1;
ListNode* prev = dummyHead;
for(int i=0; i< a-1; i++) {
cur = cur->next;
}
prev = cur;
for(int i=a-1; i< b; i++) cur = cur->next;
prev->next = list2;
while(list2->next) list2 = list2->next;
list2->next = cur->next;
return dummyHead->next;
}
};
1290.
ans <<= 1; 但 ans << 1这就是错的
class Solution {
public:
int getDecimalValue(ListNode* head) {
int ans = 0;
while(head){
ans <<= 1;
ans += head->val;
head = head->next;
}
return ans;
}
};