24 两两交换链表中的节点
解题思路
这题看了一下前面的思路就大概自己画了个图咯,自己测试了两种,奇数和偶数,一次就过了呜呜呜真不容易,注意循环条件哦
# Definition for singly-linked list.
# head = [1,2,3,4]
# 输出:[2,1,4,3]
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
headNode=ListNode()
headNode.next=head
cur=headNode
p=headNode.next
while(p!=None and p.next!=None):
cur.next=p.next
p.next=cur.next.next
cur.next.next=p
cur=p
p=cur.next
return headNode.next
if __name__=="__main__":
node1=ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
node1.next=node2
node2.next=node3
node3.next=node4
solu=Solution()
node=solu.swapPairs(node1)
print(node)
19 删除链表的倒数第N个节点
解题思路
这题只能说是非常巧妙了,我自己想的时候我想到的是让快指针去往后走几步,然后再回退,视频中老师讲到的是让两个保持 n 的间距,这样就可以实现一次遍历了,真是太强了
这个虽然一次过了但是咋个效果不好呢
# Definition for singly-linked list.
# head = [1,2,3,4,5], n = 2
# 输出:[1,2,3,5]
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if n==0:
return head
headNode=ListNode()
headNode.next=head
cur=headNode
p=cur
while(p.next!=None):
if n!=0:
p=p.next
n-=1
else:
cur=cur.next
p=p.next
if(p.next==None and n==0):
cur.next= cur.next.next
return headNode.next
if __name__=="__main__":
node1=ListNode(1)
# node2 = ListNode(2)
# node3 = ListNode(3)
# node4 = ListNode(4)
# node5 = ListNode(5)
# node1.next=node2
# node2.next=node3
# node3.next=node4
# node4.next=node5
n=2
solu=Solution()
node=solu.removeNthFromEnd(node1,0)
print(node)
卡哥的写法感觉和我也差不多,可是他的好快,应该是循环里面每次都要判断吧…
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
head_dummy = ListNode()
head_dummy.next = head
slow, fast = head_dummy, head_dummy
while(n>=0): #fast先往前走n+1步
fast = fast.next
n -= 1
while(fast!=None):
slow = slow.next
fast = fast.next
#fast 走到结尾后,slow的下一个节点为倒数第N个节点
slow.next = slow.next.next #删除
return head_dummy.next
面试题 02 07 链表相交
解题思路
对齐,然后将长的移动到和短的相等,然后一起移动
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
sizeA=0
sizeB=0
a,b=ListNode(),ListNode()
a.next=headA
b.next=headB
while a.next:
a=a.next
sizeA+=1
while b.next:
b = b.next
sizeB += 1
if sizeA>0 and sizeB>0:
if sizeA>sizeB:
a=headB
b=headA
else:
a=headA
b=headB
n=abs(sizeA-sizeB)
while n>0:
b=b.next
n-=1
while a and b:
if a==b:
return a
a=a.next
b=b.next
return None
else:
return None
142 环形链表II
解题思路
一定要看一下资料和视频,讲的相当清楚,首先这道题是有两个问题的,第一个是是否有环,第二个是环的入口在哪里.
首先是确定是否有环,是用的快慢指针,然后环内相遇
然后循环的入口
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow,fast=head,head
while fast and fast.next:
fast=fast.next.next
slow=slow.next
if fast==slow:
p=head
q=slow
while p!=q:
p=p.next
q=q.next
return q
return None
(代码自己写了一遍虽然是先看的,不过就是说一模一样,链接在上方资料处)