Leetcode-Linked List链表(总结ing)
2019-07-10 10:52
106 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yyrrbb_1/article/details/95043950
记得Class Solution
2. Add Two Numbers
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: res = node = ListNode(0) while l1 or l2: if l1: node.val += l1.val l1 = l1.next if l2: node.val += l2.val l2 = l2.next tmp = node.val // 10 node.val = node.val % 10 if l1 or l2 or tmp > 0: node.next = ListNode(tmp) node = node.next return res
19. Remove Nth Node From End of List
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: if not head.next: return None i=1 l=0 res=node=head while node.next: node=node.next i+=1 if not node.next: l=i node.next=head if i==2*l-n: node.next=node.next.next if i==2*l-1: res,node.next=node.next,None return res
21. Merge Two Sorted Lists
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val<=l2.val: l1.next=self.mergeTwoLists(l1.next,l2) return l1 else: l2.next=self.mergeTwoLists(l1,l2.next) return l2
61. Rotate List
def rotateRight_1(self, head, k): if not head or not head.next or k==0: return head l=0 node=head while node: node=node.next l+=1 if k%l==0: return head n=1 m=l-k%l node=head new_head=head while node: if n==m: new_head=node.next node.next=None node=new_head if not node.next: node.next=head break node=node.next n+=1 return new_head def rotateRight_2(self,head,k): if not head or not head.next or k==0: return head l=1 node=head while node.next: node=node.next l+=1 m=l-k%l if m==0: return head node.next=head for i in range(m): node=node.next new_head=node.next node.next=None return new_head
82. Remove Duplicates from Sorted List II
def deleteDuplicates(self, head: ListNode) -> ListNode: if not head or not head.next: return head first=ListNode(0) first.next=head pre=first pro=head while pro and pro.next: if pro.val==pro.next.val: while pro.next and pro.val==pro.next.val: pro=pro.next pro=pro.next pre.next=pro else: pre=pre.next pro=pro.next return first.next
83. Remove Duplicates from Sorted List
def deleteDuplicates(self, head: ListNode) -> ListNode: if not head or not head.next: return head node=head while node: if node.next and node.val==node.next.val: node.next=node.next.next else: node=node.next return head
142. Linked List Cycle II
def detectCycle(self, head:ListNode): if not head or not head.next: return None fast=head slow=head while fast and fast.next: fast=fast.next.next slow=slow.next if fast.val==slow.val: break if not fast or not fast.next: return None while fast.val!=head.val: fast=fast.next head=head.next return fast
160. Intersection of Two Linked Lists
def getIntersectionNode(self, headA, headB): if not headA or not headB: return None nodeA=headA while nodeA.next: nodeA=nodeA.next nodeA.next=headB fast=headA slow=headA while fast and fast.next: fast=fast.next.next slow=slow.next if fast is slow: break while fast and fast is not headA : fast=fast.next headA=headA.next nodeA.next=None return fast
206. Reverse Linked List
def reverseList_iteratively(self, head: ListNode) -> ListNode: if not head: return head node=head tmp=None while node : new_node=node.next node.next=tmp tmp=node node=new_node return tmp def reverseList_recursively(self, head: ListNode) -> ListNode: return self.recursive(None,head) def recursive(self,head:ListNode,headNext:ListNode)->ListNode: if not headNext: return head tmp=headNext.next headNext.next=head return self.recursive(headNext,tmp)
203. Remove Linked List Elements
def removeElements(self, head, val): start=head node=head while node: if start.val==val: start=start.next node=node.next continue if node.next and node.next.val==val: node.next=node.next.next else: node=node.next return start
234. Palindrome Linked List
def isPalindrome(self, head: ListNode) -> bool: if not head or not head.next: return True fast=head slow=head pre=[slow] while fast and fast.next: fast=fast.next.next slow=slow.next if fast: pre.append(slow) for i in range(len(pre)): if pre[-i-1].val!=slow.val: return False slow=slow.next return True
237. Delete Node in a Linked List
def deleteNode(self, node): if not node or not node.next: return node.val=node.next.val node.next=node.next.next
相关文章推荐
- [分析总结:leetcode-Palindrome Linked List] 给定单链表,判断链表是否为回文。
- LeetCode Delete Node in a Linked List 链表
- leetcode_c++:链表:Remove Linked List Elements(203)
- [LeetCode] Reverse Linked List II 倒置链表之二
- leetcode 206. Reverse Linked List (翻转一个链表)
- leetcode 92. Reverse Linked List II 反转链表 + 头插入反转链表(或者使用栈)
- 【题解】【链表】【Leetcode】Linked List Cycle II
- LeetCode | Linked List Cycle(判断链表是否有环)
- [LeetCode] 369. Plus One Linked List 链表加一运算
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- [leetcode]Reverse Linked List II (反转链表值 C语言实现)
- Leetcode Linked List Problem 链表问题合集
- 237[leetcode]删除链表中的节点(Delete Node in a Linked List)
- [LeetCode]79. Delete Node in a Linked List删除链表节点
- [LeetCode] Palindrome Linked List 回文链表
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
- [LeetCode] Remove Linked List Elements 移除链表元素
- leetcode之92. Reverse Linked List II(C++读错题版本,交换一个链表中指定的两个位置上的元素)
- Leetcode 链表 Linked List Cycle II
- scala反转链表 leetcode 206 Reverse Linked List