acwing-week-2-链表-Leetcode-19,237,83,61,24,206,92,160,142,148

目录

Leetcode-19

Leetcode-237

Leetcode-83

Leetcode-61

Leetcode-24

Leetcode-206

Leetcode-92

Leetcode-160

Leetcode-142

Leetcode-148


Leetcode-19

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        auto dummy = new ListNode(-1);
        dummy -> next = head;
        
        auto first = dummy, second = dummy;
        while(n--) first = first -> next;
        while(first -> next){
        	first = first -> next;
        	second = second -> next;
        }
        second -> next = second -> next -> next;
        return dummy -> next;
    }
};

Leetcode-237

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

// 用下一个节点的 val 代替此节点的 val,再删除下一个节点,从而达到在链表中删去此节点的目的。
class Solution {
public:
    void deleteNode(ListNode* node) {
        node -> val = node -> next -> val;
        node -> next = node -> next -> next;
        // *(node) = *(node -> next); // 将 node 的地址更新为 node -> next 的地址
    }
};

Leetcode-83

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        auto cur = head;
        while(cur){
        	if(cur -> next && cur -> next -> val == cur -> val)
        		cur -> next = cur -> next -> next;
        	else cur = cur -> next;
        }
        return head;
    }
};

Leetcode-61

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(!head) return NULL;

        int n = 0;
        for(auto p = head; p; p = p -> next)
        	n++;
        k %= n;
        auto first = head, second = head;
        while(k--) first = first -> next;
        while(first -> next){
        	first = first -> next;
        	second = second -> next;
        }

        first -> next = head;
        head = second -> next;
        second -> next = NULL;

        return head;
    }
};

Leetcode-24

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        auto dummy = new ListNode(-1);
        dummy -> next = head;

        for(auto p = dummy; p -> next && p -> next -> next;){
        	auto a = p -> next, b = a -> next;
        	p -> next = b;
        	a -> next = b -> next;
        	b -> next = a;
        	p = a;
        }
        return dummy -> next;
    }
};

Leetcode-206

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(!head) return NULL;

        auto a = head, b = head -> next;
        while(b){
          auto c = b -> next;
          b -> next = a;
          a = b, b = c;
        }
        head -> next = NULL;
        return a; 
    }
};


// class Solution {
// public:
//        ListNode* reverseList(ListNode* head) {
//               ListNode* new_head = NULL;
//               while (head) {
//                       ListNode* next = head->next;
//                       head->next = new_head;
//                       new_head = head;
//                       head = next;
//               }
//               return new_head;
//        }
// };

Leetcode-92

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(m == n) return head;

        auto dummy = new ListNode(-1);
        dummy -> next = head;

        auto a = dummy, d = dummy;
        for(int i = 0; i < m - 1; i++) a = a -> next;
        for(int i = 0; i < n; i++) d = d -> next;

        auto b = a -> next, c = d -> next;

        for(auto p = b, q = b -> next; q != c;){
            auto o = q -> next;
            q -> next = p;
            p = q, q = o;
        }
        b -> next = c;
        a -> next = d;
        return dummy -> next;
    }
};
// class Solution {
// public:
//   ListNode* reverseBetween(ListNode* head, int m, int n) {
//     int change_len = n - m + 1;//计算需要逆置的节点个数
//     ListNode* pre_head = NULL;//初始化开始逆置的节点的前驱
//     ListNode* result = head;//最终转化后的链表的头节点,非特殊情况即为head
//     while (head && --m) {//head向前移动m-1个位置
//       pre_head = head;
//       head = head->next;
//     }
//     ListNode* modify_list_tall = head;//将modify_list_tall指向当前的head,即逆置后的链表尾
//     ListNode* new_head = NULL;
//     while (head && change_len) {//逆置change_len个节点
//       ListNode* next = head->next;
//       head->next = new_head;
//       new_head = head;
//       head = next;
//       change_len--;
//     }
//     modify_list_tall->next = head;
//     if (pre_head) {
//       pre_head->next = new_head;
//     }
//     else {
//       result = new_head;//如果head不为空,说明不是从第一个节点开始逆置的m>1
//     }
//     return result;
//   }
// };

Leetcode-160

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        auto p = headA, q = headB;
        while(p != q){
        	if(p) p = p -> next;
        	else p = headB;
        	if(q) q = q -> next;
        	else q = headA;
        }
        return p;
    }
};

Leetcode-142

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        auto fast = head, slow = head;
        while(slow){
        	fast = fast -> next;
        	slow = slow -> next;
        	if(slow) slow = slow -> next;
        	else break;

        	if(fast == slow){
        		slow = head;
        		while(slow != fast){
        			fast = fast -> next;
        			slow = slow -> next;
        		}
        		return fast;
        	}
        }
        return NULL;
    }
};

// class Solution {
// public:
//        ListNode* detectCycle(ListNode* head) {
//               ListNode* fast = head;
//               ListNode* slow = head;
//               ListNode* meet = NULL;//相遇节点
//               while (fast) {
//                       slow = slow->next;
//                       fast = fast->next;
//                       if (!fast) {
//                              return NULL;
//                       }
//                       fast = fast->next;
//                       if (fast == slow) {
//                              meet = fast;
//                              break;
//                       }
//               }
//               if (meet == NULL) {
//                       return NULL;
//               }
//               while (head && meet) {
//                       if (meet == head) {
//                              return head;
//                       }
//                       head = head->next;
//                       meet = meet->next;
//               }
//               return NULL;
//        }
// };

Leetcode-148

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
    	int n = 0;
    	for(auto p = head; p; p = p -> next) n++;

        auto dummy = new ListNode(-1);
        dummy -> next = head;

        for(int i = 1; i < n; i *= 2){
        	auto cur = dummy;
        	for(int j = 0; j + i < n; j += i * 2){
        		auto left = cur -> next, right = cur -> next;
        		for(int k = 0; k < i; k++) right = right -> next;
        		int l = 0, r = 0;
	        	while(l < i && r < i && right){
	        		if(left -> val <= right -> val){
	        			cur -> next = left;
	        			cur = left;
	        			left = left -> next;
	        			l++;
	        		}
	        		else{
	        			cur -> next = right;
	        			cur = right;
	        			right = right -> next;
	        			r++;
	        		}
	        	}
	        	while(l < i){
	        			cur -> next = left;
	        			cur = left;
	        			left = left -> next;
	        			l++;
	        		}
	        		while(r < i && right){
	        			cur -> next = right;
	        			cur = right;
	        			right = right -> next;
	        			r++;
	        		}
	        		cur -> next = right;
        	}
        }
        return dummy -> next;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值