您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: