链表的一些leetcode题目+python(c++)
主要常見下面幾個知識點:?
1-1.請編寫一個函數,使其可以刪除某個鏈表中給定的(非末尾)節點,你將只被給定要求被刪除的節點。
python:
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def deleteNode(self, node):""":type node: ListNode:rtype: void Do not return anything, modify node in-place instead."""node.val = node.next.valnode.next = node.next.nextc++實現:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:void deleteNode(ListNode* node) {node->val = node->next->val;node->next = node->next->next;} };1-2.刪除排序鏈表中的重復元素
通過將結點的值與它之后的結點進行比較來確定它是否為重復結點。如果它是重復的,我們更改當前結點的 next 指針,以便它跳過下一個結點并直接指向下一個結點之后的結點。如果不是重復的,則節點進行下移。
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:node = headwhile node and node.next:if node.val == node.next.val:node.next = node.next.nextelse:node = node.nextreturn headc++實現:
/*** 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* new_node;Solution(){};Solution(const Solution& sol){this->new_node = new ListNode(0, NULL);this->new_node = sol.new_node;}//深拷貝 堆區開辟空間重新賦值 解決釋放內存的問題ListNode* deleteDuplicates(ListNode* head) {this->new_node = head;while(this->new_node && this->new_node->next){if(this->new_node->val == this->new_node->next->val){this->new_node->next = this->new_node->next->next;}else{this->new_node = this->new_node->next;}}return head;}virtual ~Solution(){if(this->new_node !=NULL){this->new_node = NULL;}} };1-3.刪除排序鏈表中的重復元素 II
思路:通過while循環一直去掉重復的元素
python:
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:dummy_head = ListNode(0, head)cur = dummy_headwhile cur.next and cur.next.next:if cur.next.val == cur.next.next.val:x = cur.next.valwhile cur.next and cur.next.val == x:cur.next = cur.next.nextelse:cur = cur.nextreturn dummy_head.nextc++實現:?
/*** 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* dummy_head;ListNode* cur_node;Solution(){};Solution(const Solution& sol){dummy_head = sol.dummy_head;cur_node = sol.cur_node;}ListNode* deleteDuplicates(ListNode* head) {dummy_head = new ListNode(0, head);cur_node = dummy_head;while(cur_node->next && cur_node->next->next){if(cur_node->next->val == cur_node->next->next->val){int x = cur_node->next->val;while(cur_node->next && cur_node->next->val == x){cur_node->next = cur_node->next->next;}}else{cur_node = cur_node->next;}}return dummy_head->next;}virtual ~Solution(){if(dummy_head != NULL){delete dummy_head;dummy_head = NULL;}if(cur_node != NULL){cur_node = NULL;}} };1-4.刪除鏈表的倒數第N個節點
思路:找到鏈表長度,通過在頭結點補充一個節點找到要刪除的節點的上一個節點,然后在進行刪除
方法1:循環
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:length = 0 node = head#獲取鏈表長度while node:length+=1node= node.next# print(length)curr_length = 0new_head = ListNode(0)new_head.next = headnode2=new_headstop_length = length - n#循環走到要刪除節點的前一個節點while stop_length:stop_length-=1node2 = node2.next#跳過要刪除的節點即可node2.next = node2.next.nextreturn new_head.next方法2:遞歸
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def __init__(self):self.count = 0def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:if not head: return head head.next = self.removeNthFromEnd(head.next, n) # 遞歸調用self.count += 1 # 回溯時進行節點計數return head.next if self.count == n else head方法3:雙指針
fist 指針與second指針相隔n,這樣first跑到尾部,second的下一個節點就是倒數第n個
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:# def __init__(self):# self.count = 0def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:new_head = ListNode(0)new_head.next = headfirst = headsecond = new_headfor i in range(n):first = first.nextwhile first:first = first.nextsecond = second.nextsecond.next = second.next.nextreturn new_head.nextc++實現:?
/*** 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* removeNthFromEnd(ListNode* head, int n) {ListNode* new_head = new ListNode(0);new_head ->next = head;ListNode* first = head;ListNode* second = new_head;for(int i=0;i<n;i++){first = first->next;}while(first){first = first->next;second = second->next;}second->next = second->next->next;return new_head->next;} };1-5. 刪除鏈表的節點
思路:雙指針
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution:def deleteNode(self, head: ListNode, val: int) -> ListNode:if head is None:return headnew_head = ListNode(0)new_head.next = headpre = new_headcur = headwhile cur.val != val:pre = curcur = cur.nextpre.next = cur.nextreturn new_head.nextc++實現:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* deleteNode(ListNode* head, int val) {if(head == NULL){return NULL;}ListNode* new_head = new ListNode(0);new_head->next = head;ListNode* pre = new_head;ListNode* cur = head;while(cur->val != val){pre = cur;cur = cur->next;}pre->next = cur->next;return new_head->next;} };1-6.兩兩交換鏈表中的節點
思路:迭代法
python代碼:?
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def swapPairs(self, head: ListNode) -> ListNode:new_head = ListNode(0)new_head.next = headtemp = new_headwhile temp.next and temp.next.next:node1 = temp.nextnode2 = temp.next.nexttemp.next = node2node1.next = node2.nextnode2.next = node1temp = node1return new_head.nextc++實現:
/*** 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) {ListNode* new_head = new ListNode(0);new_head->next = head;ListNode* temp = new_head;while(temp->next && temp->next->next){ListNode* head1 = temp->next;ListNode* head2 = temp->next->next;temp->next = head2;head1->next = head2->next;head2->next = head1;temp = head1;}return new_head->next;} };思路:遞歸法
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def swapPairs(self, head: ListNode) -> ListNode:if head is None or head.next is None:return headnew_head = head.nexthead.next = self.swapPairs(new_head.next)new_head.next = headreturn new_headc++實現:?
/*** 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 == nullptr || head->next == nullptr){return head;}ListNode* new_head;new_head = head->next;head->next = swapPairs(new_head->next);new_head->next = head;return new_head;} };2-1.反轉鏈表
思路1:雙指針?
class Solution(object):def reverseList(self, head):""":type head: ListNode:rtype: ListNode"""# 申請兩個節點,pre和 cur,pre指向Nonepre = Nonecur = head# 遍歷鏈表,while循環里面的內容其實可以寫成一行while cur:# 記錄當前節點的下一個節點tmp = cur.next# 然后將當前節點指向precur.next = pre# pre和cur節點都前進一位pre = curcur = tmpreturn prec++實現:
/*** 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* temp = head;while(head){temp = head->next;head->next = pre;pre = head;head = temp;}return pre;} };思路2.遞歸法
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def reverseList(self, head: ListNode) -> ListNode:# pre = None# cur = head# while cur:# node = cur.next# cur.next = pre# pre = cur# cur = node# return preif head is None or head.next is None:return headnew_node = self.reverseList(head.next)print('head.val',head.val)head.next.next = headhead.next = Nonereturn new_node2-2:旋轉鏈表
思路:
構成循環列表以后 找到移動的節點 在拆分
python:
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def rotateRight(self, head: ListNode, k: int) -> ListNode:if k == 0 or head is None or head.next is None:return head#計算鏈表長度cur = head n = 1while cur.next:cur = cur.nextn += 1# print('==n:', n) add = n - k % nif add == n:#k是n的整數倍直接返回原節點return headcur.next = head #構成環while add:cur = cur.nextadd -= 1new_head = cur.next#找到移動后的開始節點cur.next = None#拆開return new_headc++實現:
/*** 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* rotateRight(ListNode* head, int k) {if(k == 0 || head == nullptr || head->next == nullptr){return head;}int n = 1;//得到環長度ListNode* cur = head;while(cur->next){cur = cur->next;n++;}//找到移動的add長度int add = n - k % n;if(add == n){return head;}cur->next = head;//構成環while(add){cur = cur->next;add--;}ListNode* new_node = cur->next;cur->next = nullptr;//拆環return new_node;} };3-1.合并兩個排序的鏈表
思路:引入一個指針頭 python實現
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:head = ListNode(0)node = headwhile l1 and l2:if l1.val < l2.val:node.next = l1l1 = l1.nextelse:node.next = l2l2 = l2.nextnode = node.nextif l1 is not None:node.next= l1if l2 is not None:node.next= l2return head.nextc++實現:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode* new_head = new ListNode(0);ListNode* node = new_head;while(l1!=NULL && l2 !=NULL){if(l1->val<l2->val){node->next = l1;l1 = l1->next;}else{node->next = l2;l2 = l2->next; }node = node->next;}if (l1!=NULL){node->next = l1;}if(l2!=NULL){node->next = l2;}return new_head->next;} };思路:遞歸
/*** 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* mergeTwoLists(ListNode* l1, ListNode* l2) {if(l1 == nullptr){return l2;}if(l2 == nullptr){return l1;}if(l1->val < l2->val){l1->next = mergeTwoLists(l1->next, l2);return l1;}else{l2->next = mergeTwoLists(l1, l2->next);return l2;}} };3-2.合并K個升序鏈表
思路1:上一題合并兩個變成for循環順序合并
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def mergeTwo(self, l1, l2):if l1 is None:return l2if l2 is None:return l1head = ListNode(0)node = headwhile l1 and l2:if l1.val <l2.val:node.next = l1l1 = l1.nextelse:node.next = l2l2 = l2.nextnode = node.nextif l1:node.next = l1if l2:node.next = l2return head.nextdef mergeKLists(self, lists: List[ListNode]) -> ListNode:ans = Nonefor i in range(len(lists)):ans = self.mergeTwo(ans,lists[i])return ansc++:
/*** 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* mergtwo(ListNode* l1, ListNode* l2){if(l1==nullptr){return l2;}if(l2==nullptr){return l1;}ListNode* new_head= new ListNode(0);ListNode* node = new_head;while(l1 && l2){if(l1->val<l2->val){node->next = l1;l1= l1->next;}else{node->next = l2;l2= l2->next;}node = node->next;}if(l1){node->next = l1;}if(l2){node->next = l2;}return new_head->next;}ListNode* mergeKLists(vector<ListNode*>& lists) {ListNode* res = nullptr;for (int i=0;i<lists.size();i++){res = mergtwo(res,lists[i]);}return res;} };思路2:分治歸并1
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def mergeTwo(self, l1, l2):if l1 is None:return l2if l2 is None:return l1head = ListNode(0)node = headwhile l1 and l2:if l1.val <l2.val:node.next = l1l1 = l1.nextelse:node.next = l2l2 = l2.nextnode = node.nextif l1:node.next = l1if l2:node.next = l2return head.nextdef mergeSort(self, lists, left, right):if left==right:return lists[left]middle = left + (right-left)//2# print('== middle:', middle)l1 = self.mergeSort(lists,left,middle)# print('== l1:', l1)l2 = self.mergeSort(lists,middle+1,right)return self.mergeTwo(l1, l2)def mergeKLists(self, lists: List[ListNode]) -> ListNode:# print('==hahah')if len(lists)==0:return Nonereturn self.mergeSort(lists,0,len(lists) - 1)c++實現:?
/*** 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* mergetwo(ListNode* l1, ListNode* l2){if(l1==nullptr){return l2;}if(l2==nullptr){return l1;}ListNode* new_head= new ListNode(0);ListNode* node = new_head;while(l1 && l2){if(l1->val<l2->val){node->next = l1;l1= l1->next;}else{node->next = l2;l2= l2->next;}node = node->next;}if(l1){node->next = l1;}if(l2){node->next = l2;}return new_head->next;}ListNode* mergesort(vector<ListNode*>& lists,int left, int right){if(left==right){return lists[left];}int middle = left+(right -left)/2;ListNode* l1 = mergesort(lists,left,middle);ListNode* l2 = mergesort(lists,middle+1,right);return mergetwo(l1,l2);}ListNode* mergeKLists(vector<ListNode*>& lists) {// ListNode* res = nullptr;// for (int i=0;i<lists.size();i++){// res = mergtwo(res,lists[i]);// }// return res;if (lists.size()==0){return nullptr;}return mergesort(lists,0,lists.size()-1);} };思路3:分支歸并2 參考排序算法的歸并排序 排序算法--(冒泡排序,插入排序,選擇排序,歸并排序,快速排序,桶排序,計數排序,基數排序)_智障變智能-CSDN博客
class Solution:def mergeTwo(self, l1, l2):if l1 is None:return l2if l2 is None:return l1head = ListNode(0)node = headwhile l1 and l2:if l1.val < l2.val:node.next = l1l1 = l1.nextelse:node.next = l2l2 = l2.nextnode = node.nextif l1:node.next = l1if l2:node.next = l2return head.nextdef mergeSort(self, L):if len(L) <= 1:return L[0]mid = len(L) // 2 l1 = self.mergeSort(L[:mid])l2 = self.mergeSort(L[mid:])return self.mergeTwo(l1, l2)def mergeKLists(self, lists: List[ListNode]) -> ListNode:if len(lists)==0:return Nonereturn self.mergeSort(lists)3-3.合并兩個有序鏈表
思路:遞歸 終止條件就是節點為None.
1.遞歸法?
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:#遞歸的終止條件if l1 is None:return l2elif l2 is None:return l1 elif l1.val < l2.val:l1.next = self.mergeTwoLists(l1.next,l2)return l1else:l2.next = self.mergeTwoLists(l1,l2.next)return l22.迭代法:
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:fake_head_node = ListNode(0)cur = fake_head_nodewhile l1 and l2:if l1.val<l2.val:cur.next = l1l1 = l1.nextelse:cur.next = l2l2 = l2.next cur = cur.nextif l1:cur.next = l1else:cur.next = l2return fake_head_node.next3-4.排序鏈表
思路1:歸并排序 先通過快慢指針找到中心點 進行截斷以后一直遞歸拆開 在進行合并即可
python代碼:
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def merge(self, left, right):res = ListNode(0)temp = reswhile left and right:if left.val < right.val:temp.next, left = left, left.nextelse:temp.next, right = right, right.nexttemp = temp.nexttemp.next = left if left else rightreturn res.nextdef sortList(self, head: ListNode) -> ListNode:if head is None or head.next is None:return head #快慢指針找到鏈表中心點slow, fast = head, head.nextwhile fast and fast.next:fast, slow = fast.next.next, slow.nextmid, slow.next = slow.next, None#將找到的中心點進行截斷故 slow.next = Noneleft, right = self.sortList(head), self.sortList(mid)#遞歸一直進行拆分return self.merge(left, right)#合并操作c++代碼:
/*** 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* mergetwo(ListNode* l1,ListNode* l2){ListNode* new_head = new ListNode(0);ListNode* node = new_head;while(l1 && l2){if(l1->val<l2->val){node->next = l1;l1 = l1->next;}else{node->next = l2;l2 = l2->next;}node = node->next;}if(l1){node->next = l1;}if(l2){node->next = l2;}return new_head->next;}ListNode* sortList(ListNode* head) {if(head==nullptr || head->next==nullptr){return head;}ListNode* slow = head;ListNode* fast = head->next;while(fast && fast->next){slow = slow->next;fast = fast->next->next;}ListNode* middle = slow->next;slow->next = nullptr;ListNode* l1 = sortList(head);ListNode* l2 = sortList(middle);return mergetwo(l1,l2);} };思路2:合并也是遞歸
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:# def merge(self, left, right):# res = ListNode(0)# temp = res# while left and right:# if left.val < right.val:# temp.next, left = left, left.next# else:# temp.next, right = right, right.next# temp = temp.next# temp.next = left if left else right# return res.nextdef merge(self,left,right):if left is None:return rightif right is None:return leftif left.val < right.val:left.next = self.merge(left.next, right)return leftelse:right.next = self.merge(left, right.next)return rightdef sortList(self, head: ListNode) -> ListNode:if head is None or head.next is None:return head #快慢指針找到鏈表中心點slow, fast = head, head.nextwhile fast and fast.next:fast, slow = fast.next.next, slow.nextmid, slow.next = slow.next, None#將找到的中心點進行截斷故 slow.next = Noneleft, right = self.sortList(head), self.sortList(mid)#遞歸一直進行拆分return self.merge(left, right)#合并操作3-5.兩數相加?
思路:開出一個head頭,利用一個指針進行遍歷,需要注意的是進位
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:head = ListNode(0)new_node = headcarry = 0while l1 and l2:new_node.next =ListNode(l1.val+l2.val+carry)carry = new_node.next.val//10 new_node.next.val = new_node.next.val%10l1 = l1.nextl2= l2.nextnew_node = new_node.next# print(carry)while l1:new_node.next = ListNode(l1.val+carry)carry = new_node.next.val//10new_node.next.val = new_node.next.val%10l1 = l1.nextnew_node = new_node.nextwhile l2:new_node.next = ListNode(l2.val+carry)carry = new_node.next.val//10new_node.next.val = new_node.next.val%10l2 = l2.nextnew_node = new_node.nextif carry:new_node.next = ListNode(carry)return head.nextc++實現:
/*** 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* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* head = new ListNode(0);ListNode* new_head = head;int carry = 0;while(l1 && l2){new_head->next = new ListNode(l1->val + l2->val + carry);carry = new_head->next->val/10;new_head->next->val = new_head->next->val%10; new_head = new_head->next;l1 = l1->next;l2 = l2->next;}while(l1){new_head->next = new ListNode(l1->val + carry);carry = new_head->next->val/10;new_head->next->val = new_head->next->val%10; new_head = new_head->next;l1 = l1->next;}while(l2){new_head->next = new ListNode(l2->val + carry);carry = new_head->next->val/10;new_head->next->val = new_head->next->val%10; new_head = new_head->next;l2 = l2->next;}if(carry){new_head->next = new ListNode(carry);}return head->next;} };3-5.2?兩數相加 II
思路:在上一題基礎上加一個棧
python:
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:value1,value2 = [], []while l1:value1.append(l1.val)l1 = l1.nextwhile l2:value2.append(l2.val)l2 = l2.nextcarry = 0res = Nonewhile len(value1) > 0 or len(value2) > 0 or carry != 0:if len(value1):temp_1 = value1.pop()else:temp_1 = 0if len(value2):temp_2 = value2.pop()else:temp_2 = 0cur = temp_1 + temp_2 + carrycarry = cur // 10cur %= 10node = ListNode(cur)node.next = resres = nodereturn resc++實現:
/*** 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* addTwoNumbers(ListNode* l1, ListNode* l2) {stack<int> value1, value2;while(l1){value1.push(l1->val);l1 = l1->next;}while(l2){value2.push(l2->val);l2 = l2->next;}int carry = 0;ListNode* res = nullptr;while(!value1.empty() || !value2.empty() || carry != 0){int temp_1 = 0, temp_2 = 0;if(!value1.empty()){temp_1 = value1.top();value1.pop();}if(!value2.empty()){temp_2 = value2.top();value2.pop();}int cur = temp_1 + temp_2 + carry;carry = cur / 10;cur %= 10;ListNode* node = new ListNode(cur);node->next = res;res = node;}return res;} };3-6.合并兩個鏈表
思路:找出被截斷的兩個鏈表點,開始節點指向要加入的鏈表,加入的鏈表在指向結束節點就行。
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:node = list1length = 0while list1:if length == a - 1:start = list1if length == b + 1:end = list1length += 1 list1 = list1.nextstart.next = list2while list2.next:list2 = list2.nextlist2.next = endreturn nodec++實現:
/*** 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* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {ListNode* node = list1;ListNode* start; ListNode* end;int length = 0;while(list1){if(length == a-1){start = list1;}if(length == b+1){end = list1;}length++;list1 = list1->next;}start->next = list2;while(list2->next){list2 = list2->next;}list2->next = end;return node;} };3-7. 重排鏈表
思路:利用線性表存儲該鏈表節點,然后利用線性表可以下標訪問的特點,直接按順序訪問指定元素,重建該鏈表即可。
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution:def reorderList(self, head: ListNode) -> None:"""Do not return anything, modify head in-place instead."""nodes_list = []node = headwhile node:nodes_list.append(node)node = node.nextstart, end = 0, len(nodes_list) - 1while start < end:nodes_list[start].next = nodes_list[end]start += 1if start == end:breaknodes_list[end].next = nodes_list[start]end -= 1nodes_list[start].next = Nonec++實現:
/*** 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:void reorderList(ListNode* head) {vector<ListNode*> nodes_list;ListNode* node = head;while(node){nodes_list.push_back(node);node = node->next;}int start = 0, end = nodes_list.size() - 1;while(start < end){nodes_list[start]->next = nodes_list[end];start++;if(start == end){break;}nodes_list[end]->next = nodes_list[start];end--;}nodes_list[start]->next = nullptr;} };4-1.環形鏈表
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def hasCycle(self, head: ListNode) -> bool:#快慢指針 人追人slow,fast = head,headwhile fast and fast.next:fast = fast.next.nextslow = slow.nextif slow==fast:return True return Falsec++實現:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:bool hasCycle(ListNode *head) {ListNode* slow = head;ListNode* fast = head;while(fast && fast->next){slow = slow->next;fast = fast->next->next;if(slow == fast){return true;}}return false;} };4-2.給定一個有環鏈表,實現一個算法返回環路的開頭節點。
假設有兩個指針,分別為快慢指針fast和slow, 快指針每次走兩步,慢指針每次前進一步,如果有環則兩個指針必定相遇;
反證法:假設快指針真的 越過 了慢指針,且快指針處于位置 i+1,而慢指針處于位置 i,那么在前一步,快指針處于位置 i-1,慢指針也處于位置 i-1,它們相遇了。
A:鏈表起點
B:環起點
C:相遇點
X:環起點到相遇點距離
Y:鏈表起點到環起點距離
R:環的長度
S:第一次相遇時走過的路程
1.慢指針slow第一次相遇走過的路程 S1 = Y + X;(11)
快指針fast第一次相遇走過的路程 S2=2S1 = Y + X + NR;(2)
說明:快指針的速度是慢指針的兩倍,相同時間內路程應該是慢指針的兩倍,Y + X + NR是因為快指針可能經過N圈后兩者才相遇;
把(1)式代入(2)式得:Y = NR -X;?
2..在將慢指針回到A點,滿指針和快指針同時走,在B點相遇,此處就是環節點.
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def detectCycle(self, head: ListNode) -> ListNode:slow = headfast = head;while fast:if fast and fast.next:slow = slow.nextfast = fast.next.nextelse:return Noneif slow==fast:breakif fast ==None or fast.next==None:return Noneslow= headwhile slow!=fast:slow = slow.nextfast = fast.nextreturn slowc++實現:
/*** 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) {ListNode* slow = head;ListNode* fast = head;while(fast){if(fast && fast->next){slow = slow->next;fast = fast->next->next;}else{return NULL;}if(slow==fast){break;}}if(!fast || !fast->next){return NULL;}slow = head;while(slow!=fast){slow = slow->next;fast = fast->next;}return slow;} };4-3.鏈表相交
如這題應該是比較明顯的雙指針題,要是能實現一種算法讓兩個指針分別從A和B點往C點走,兩個指針分別走到C后,又各自從另外一個指針的起點,也就是A指針第二次走從B點開始走,B指針同理,這樣,A指針走的路徑長度 AO + OC + BO 必定等于B指針走的路徑長度 BO + OC + AO,這也就意味著這兩個指針第二輪走必定會在O點相遇,相遇后也即到達了退出循環的條件,代碼如下:
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:index_a = headAindex_b = headBwhile index_a !=index_b:if index_a !=None:index_a = index_a.nextelse:index_a = headBif index_b != None:index_b = index_b.nextelse:index_b = headAreturn index_ac++實現:
/*** 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) {ListNode* node_A = headA;ListNode* node_B = headB;while(node_A!=node_B){if(node_A!=NULL){node_A=node_A->next;}else{node_A = headB;}if(node_B!=NULL){node_B=node_B->next;}else{node_B = headA;}}return node_A;} };4-4.兩個鏈表的第一個公共節點
思路:雙指針 兩個指針輪流走一遍各自的路程,這樣相遇就是公共節點,對于沒有公共節點的情況,所以需要判斷自身節點不是none,而不是.next是none,在去交換指針,否則會陷入無窮循環,而此時輸出就是none。
python
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:first_head = headAsecond_head = headBwhile first_head !=second_head:if first_head is not None:first_head = first_head.next else:first_head = headBif second_head is not None:second_head = second_head.nextelse:second_head = headA# print(first_head)return first_headc++:
/*** 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) {ListNode *first_node;first_node = headA;ListNode *second_node;second_node= headB;while(first_node != second_node){if(first_node !=NULL){first_node = first_node->next;}else{first_node = headB;}if(second_node !=NULL){second_node = second_node->next;}else{second_node = headA;}}return first_node;} };5-1.輸入一個鏈表的頭節點,從尾到頭反過來返回每個節點的值(用數組返回)。
1.借用棧
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def reversePrint(self, head: ListNode) -> List[int]:stack = []while head:stack.append(head.val)head = head.nextreturn stack[::-1]c++:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:vector<int> reversePrint(ListNode* head) {vector<int> res;while(head){res.push_back(head->val);head = head->next;}reverse(res.begin(),res.end());return res;} };2.遞歸回溯
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def reversePrint(self, head: ListNode) -> List[int]:if head:return self.reversePrint(head.next)+[head.val]else:return []5-2.請判斷一個鏈表是否為回文鏈表
利用列表將列表值進行拷貝,在判斷是否是回文字符串
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def isPalindrome(self, head: ListNode) -> bool:stack= []while head:stack.append(head.val)head = head.nextreturn stack==stack[::-1]c++實現:
/*** 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:bool isPalindrome(ListNode* head) {vector<int> res;while(head){res.push_back(head->val);head = head->next;}int left=0;int right=res.size()-1;while(left<right){if(res[left]==res[right]){left+=1;right-=1;}else{return false;}}return true;} };5-3.分隔鏈表
思路:開出兩個大小節點,用于指向大于x和小于x的,遍歷結束以后在合并即可
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Noneclass Solution:def partition(self, head: ListNode, x: int) -> ListNode:small_head = ListNode(0)large_head = ListNode(0)small_node = small_headlarge_node = large_headwhile head:if head.val < x:small_node.next = headsmall_node = small_node.nextelse:large_node.next = headlarge_node = large_node.nexthead= head.nextlarge_node.next = Nonesmall_node.next = large_head.nextreturn small_head.nextc++寫法:
/*** 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* partition(ListNode* head, int x) {ListNode* small_head = new ListNode(0);ListNode* big_head = new ListNode(0);ListNode* small_node =small_head;ListNode* big_node = big_head;while(head){if (head->val<x){small_node->next = head; small_node = small_node->next;}else{big_node->next = head;big_node = big_node->next;}head = head->next;}big_node->next = nullptr;small_node->next = big_head->next;return small_head->next;} };6-1.二叉樹展開為鏈表
思路:可看出是根據前序遍歷的節點統統放在右子樹上
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution:def help(self, node):if node is not None:self.res.append(node)self.help(node.left)self.help(node.right)def flatten(self, root: TreeNode) -> None:"""Do not return anything, modify root in-place instead."""self.res = []self.help(root)# print(self.res)length = len(self.res)for i in range(1,length):pre,cur = self.res[i-1],self.res[i]pre.left = Nonepre.right = curreturn rootc++實現:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/ class Solution { public:vector<TreeNode* >res;void help(TreeNode* node){if(node){res.push_back(node);help(node->left);help(node->right);}}void flatten(TreeNode* root) {help(root);for(int i=1;i<res.size();i++){TreeNode* pre = res[i-1];TreeNode* cur = res[i];pre->left = nullptr;pre->right = cur;}// return root;} };雙向鏈表例題:
7-1:LRU 緩存機制
思路:雙向鏈表,這樣優先將get的移到頭部
class Node:def __init__(self, key=0, value=0):self.key = keyself.value = valueself.prev = Noneself.next = Noneclass LRUCache:def __init__(self, capacity: int):self.cache = {}self.head = Node()self.tail = Node()self.head.next = self.tailself.tail.prev = self.headself.capacity = capacityself.size = 0def get(self, key: int) -> int:if key not in self.cache:return -1# 如果 key 存在,先通過哈希表定位,再移到頭部node = self.cache[key]self.moveToHead(node)return node.valuedef put(self, key: int, value: int) -> None:if key not in self.cache:# 如果 key 不存在,創建一個新的節點node = Node(key, value)# 添加進哈希表self.cache[key] = node# 添加至雙向鏈表的頭部self.addToHead(node)self.size += 1if self.size > self.capacity:# 如果超出容量,刪除雙向鏈表的尾部節點removed = self.removeTail()# 刪除哈希表中對應的項self.cache.pop(removed.key)self.size -= 1else:# 如果 key 存在,先通過哈希表定位,再修改 value,并移到頭部node = self.cache[key]node.value = valueself.moveToHead(node)def addToHead(self, node):node.prev = self.headnode.next = self.head.nextself.head.next.prev = nodeself.head.next = nodedef removeNode(self, node):node.prev.next = node.nextnode.next.prev = node.prevdef moveToHead(self, node):self.removeNode(node)self.addToHead(node)def removeTail(self):node = self.tail.prevself.removeNode(node)return node總結
以上是生活随笔為你收集整理的链表的一些leetcode题目+python(c++)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Opencv中Mat的data数据只定义
- 下一篇: python写日志