日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

链表的一些leetcode题目+python(c++)

發(fā)布時間:2024/7/23 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表的一些leetcode题目+python(c++) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

主要常見下面幾個知識點:?

1-1.請編寫一個函數(shù),使其可以刪除某個鏈表中給定的(非末尾)節(jié)點,你將只被給定要求被刪除的節(jié)點。

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.next

c++實現(xiàn):

/*** 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.刪除排序鏈表中的重復(fù)元素

通過將結(jié)點的值與它之后的結(jié)點進行比較來確定它是否為重復(fù)結(jié)點。如果它是重復(fù)的,我們更改當前結(jié)點的 next 指針,以便它跳過下一個結(jié)點并直接指向下一個結(jié)點之后的結(jié)點。如果不是重復(fù)的,則節(jié)點進行下移。

# 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 head

c++實現(xiàn):

/*** 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;}//深拷貝 堆區(qū)開辟空間重新賦值 解決釋放內(nèi)存的問題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.刪除排序鏈表中的重復(fù)元素 II

思路:通過while循環(huán)一直去掉重復(fù)的元素

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.next

c++實現(xiàn):?

/*** 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.刪除鏈表的倒數(shù)第N個節(jié)點

思路:找到鏈表長度,通過在頭結(jié)點補充一個節(jié)點找到要刪除的節(jié)點的上一個節(jié)點,然后在進行刪除

方法1:循環(huán)

# 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#循環(huán)走到要刪除節(jié)點的前一個節(jié)點while stop_length:stop_length-=1node2 = node2.next#跳過要刪除的節(jié)點即可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) # 遞歸調(diào)用self.count += 1 # 回溯時進行節(jié)點計數(shù)return head.next if self.count == n else head

方法3:雙指針

fist 指針與second指針相隔n,這樣first跑到尾部,second的下一個節(jié)點就是倒數(shù)第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.next

c++實現(xiàn):?

/*** 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. 刪除鏈表的節(jié)點

思路:雙指針

# 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.next

c++實現(xiàn):

/*** 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.兩兩交換鏈表中的節(jié)點

思路:迭代法

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.next

c++實現(xiàn):

/*** 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_head

c++實現(xiàn):?

/*** 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.反轉(zhuǎn)鏈表

思路1:雙指針?

class Solution(object):def reverseList(self, head):""":type head: ListNode:rtype: ListNode"""# 申請兩個節(jié)點,pre和 cur,pre指向Nonepre = Nonecur = head# 遍歷鏈表,while循環(huán)里面的內(nèi)容其實可以寫成一行while cur:# 記錄當前節(jié)點的下一個節(jié)點tmp = cur.next# 然后將當前節(jié)點指向precur.next = pre# pre和cur節(jié)點都前進一位pre = curcur = tmpreturn pre

c++實現(xiàn):

/*** 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_node

2-2:旋轉(zhuǎn)鏈表

思路:

構(gòu)成循環(huán)列表以后 找到移動的節(jié)點 在拆分

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的整數(shù)倍直接返回原節(jié)點return headcur.next = head #構(gòu)成環(huán)while add:cur = cur.nextadd -= 1new_head = cur.next#找到移動后的開始節(jié)點cur.next = None#拆開return new_head

c++實現(xiàn):

/*** 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;//得到環(huán)長度ListNode* cur = head;while(cur->next){cur = cur->next;n++;}//找到移動的add長度int add = n - k % n;if(add == n){return head;}cur->next = head;//構(gòu)成環(huán)while(add){cur = cur->next;add--;}ListNode* new_node = cur->next;cur->next = nullptr;//拆環(huán)return new_node;} };

3-1.合并兩個排序的鏈表

思路:引入一個指針頭 python實現(xiàn)

# 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.next

c++實現(xiàn):

/*** 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循環(huán)順序合并

# 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 ans

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* 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++實現(xiàn):?

/*** 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 參考排序算法的歸并排序 排序算法--(冒泡排序,插入排序,選擇排序,歸并排序,快速排序,桶排序,計數(shù)排序,基數(shù)排序)_智障變智能-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.合并兩個有序鏈表

思路:遞歸 終止條件就是節(jié)點為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 l2

2.迭代法:

# 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.next

3-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.兩數(shù)相加?

思路:開出一個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.next

c++實現(xiàn):

/*** 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?兩數(shù)相加 II

思路:在上一題基礎(chǔ)上加一個棧

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 res

c++實現(xiàn):

/*** 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.合并兩個鏈表

思路:找出被截斷的兩個鏈表點,開始節(jié)點指向要加入的鏈表,加入的鏈表在指向結(jié)束節(jié)點就行。

# 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 node

c++實現(xiàn):

/*** 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. 重排鏈表

思路:利用線性表存儲該鏈表節(jié)點,然后利用線性表可以下標訪問的特點,直接按順序訪問指定元素,重建該鏈表即可。

# 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 = None

c++實現(xiàn):

/*** 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.環(huán)形鏈表

# 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 False

c++實現(xiàn):

/*** 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.給定一個有環(huán)鏈表,實現(xiàn)一個算法返回環(huán)路的開頭節(jié)點。

假設(shè)有兩個指針,分別為快慢指針fast和slow, 快指針每次走兩步,慢指針每次前進一步,如果有環(huán)則兩個指針必定相遇;

反證法:假設(shè)快指針真的 越過 了慢指針,且快指針處于位置 i+1,而慢指針處于位置 i,那么在前一步,快指針處于位置 i-1,慢指針也處于位置 i-1,它們相遇了。

A:鏈表起點
B:環(huán)起點
C:相遇點
X:環(huán)起點到相遇點距離
Y:鏈表起點到環(huán)起點距離
R:環(huán)的長度
S:第一次相遇時走過的路程

1.慢指針slow第一次相遇走過的路程 S1 = Y + X;(11)
快指針fast第一次相遇走過的路程 S2=2S1 = Y + X + NR;(2)
說明:快指針的速度是慢指針的兩倍,相同時間內(nèi)路程應(yīng)該是慢指針的兩倍,Y + X + NR是因為快指針可能經(jīng)過N圈后兩者才相遇;
把(1)式代入(2)式得:Y = NR -X;?

2..在將慢指針回到A點,滿指針和快指針同時走,在B點相遇,此處就是環(huán)節(jié)點.

# 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 slow

c++實現(xiàn):

/*** 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.鏈表相交

如這題應(yīng)該是比較明顯的雙指針題,要是能實現(xiàn)一種算法讓兩個指針分別從A和B點往C點走,兩個指針分別走到C后,又各自從另外一個指針的起點,也就是A指針第二次走從B點開始走,B指針同理,這樣,A指針走的路徑長度 AO + OC + BO 必定等于B指針走的路徑長度 BO + OC + AO,這也就意味著這兩個指針第二輪走必定會在O點相遇,相遇后也即到達了退出循環(huán)的條件,代碼如下:

# 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_a

c++實現(xiàn):

/*** 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.兩個鏈表的第一個公共節(jié)點

思路:雙指針 兩個指針輪流走一遍各自的路程,這樣相遇就是公共節(jié)點,對于沒有公共節(jié)點的情況,所以需要判斷自身節(jié)點不是none,而不是.next是none,在去交換指針,否則會陷入無窮循環(huán),而此時輸出就是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_head

c++:

/*** 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.輸入一個鏈表的頭節(jié)點,從尾到頭反過來返回每個節(jié)點的值(用數(shù)組返回)。

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++實現(xiàn):

/*** 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.分隔鏈表

思路:開出兩個大小節(jié)點,用于指向大于x和小于x的,遍歷結(jié)束以后在合并即可

# 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.next

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* 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.二叉樹展開為鏈表

思路:可看出是根據(jù)前序遍歷的節(jié)點統(tǒng)統(tǒng)放在右子樹上

# 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 root

c++實現(xiàn):

/*** 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 緩存機制

思路:雙向鏈表,這樣優(yōu)先將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 不存在,創(chuàng)建一個新的節(jié)點node = Node(key, value)# 添加進哈希表self.cache[key] = node# 添加至雙向鏈表的頭部self.addToHead(node)self.size += 1if self.size > self.capacity:# 如果超出容量,刪除雙向鏈表的尾部節(jié)點removed = self.removeTail()# 刪除哈希表中對應(yīng)的項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

總結(jié)

以上是生活随笔為你收集整理的链表的一些leetcode题目+python(c++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产色图视频 | 日韩精品在线观看一区二区 | 91免费 看片 | 色综合五月 | 亚洲综合激情另类小说区 | 色欲AV无码精品一区二区久久 | 91免费视 | 伦理片波多野结衣 | 艳母在线视频 | 激情综合色 | 99久久久无码国产精品性色戒 | 嫩草视频在线看 | 国产又大又粗又爽的毛片 | 精品一区二区电影 | 亚洲天堂视频在线 | 成人午夜淫片100集 伊人久久国产 | 俄罗斯嫩小性bbwbbw | 99re免费视频精品全部 | 亚洲无限观看 | 亚洲毛片在线观看 | 超碰人人爱 | 亚洲少妇一区 | 国产精品久久久久久人妻精品动漫 | 免费在线中文字幕 | av加勒比 | 国产黄a三级三级三级看三级男男 | 男女一级特黄 | 大j8福利视频导航 | 在线观看的免费 | 中国av一区二区 | 香蕉视频在线视频 | 抱着老师的嫩臀猛然挺进视频 | 国产精品91av| 热久久免费 | 德国老妇性猛交 | 免费成人黄色片 | 久久久久亚洲AV成人无在 | 亚洲av无码精品色午夜果冻不卡 | 爱情岛论坛自拍亚洲品质极速最新章 | 国产精品视频入口 | 美女日批视频在线观看 | 日韩精美视频 | 国产精品久久久久久人 | 日韩欧美国产高清91 | 男男做的视频 | 欧美一区二区三区影院 | 亚洲国产精品久久精品怡红院 | 99riav国产精品视频 | 在线免费视频一区二区 | 美女被娇喘视频 | 少妇与公做了夜伦理69 | 丰满人妻一区二区三区53 | 中文字幕123 | 美乳人妻一区二区三区 | 日韩激情片 | 日韩欧美黄色 | youjizz亚洲| 黄色自拍视频 | 国产视频一区二区三区四区五区 | 91视频a| 精品久久久久久久久久久 | 99精品久久久久久 | 国产欧美视频一区二区三区 | 亚洲精品乱码久久久久久不卡 | 亚洲av无一区二区三区 | 亚洲一区综合 | 韩国一区视频 | 精品黄网 | 亚洲精品乱码久久久久久蜜桃欧美 | 亚洲成人精选 | 一级影片在线观看 | 国产欧美日本 | 国产免费a级片 | 免费在线视频你懂的 | 色一情一乱一乱一区91av | 成人福利一区 | 日日摸日日添日日碰9学生露脸 | 国产在线国偷精品免费看 | 免费高清视频在线观看 | 久久久久成人精品免费播放动漫 | 短视频在线观看 | 丰满大乳奶做爰ⅹxx视频 | 我的好妈妈在线观看 | 日本bbwbbw | 国产成人在线一区 | 成人无码一区二区三区 | 国产噜噜噜 | 国产ts在线观看 | 日本久久爱 | 精品黑人一区二区三区久久 | 性欧美lx╳lx╳ | 欧美精品久久久久久久自慰 | 精品乱码一区内射人妻无码 | 李宗瑞91在线正在播放 | 成人免费自拍视频 | 黄色99视频| 亚洲av无码国产精品久久 | 亚洲再线 | 人人搞人人爱 |