- 由于是两两交换,那么就自然需要考虑偶数和奇数个节点的情况:偶数时curr.next = None终止;奇数时curr.next.next = None终止。
- 以上讨论同样适用于空链表的情况,因为链表节点数量是0,就符合偶数的情况。
- 如果这样写:while (curr.next.next and currr.next),那么当curr.next为None的时候,这样curr.next.next就是对空指针取值,则会出现空指针异常
由于curr是定义的dummy_head,那么就不会为空了,因此就不需要对curr进行判断。
# 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 swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy_head = ListNode(next=head)
curr = dummy_head
while curr.next and curr.next.next:
temp = curr.next
temp1 = curr.next.next.next
curr.next = curr.next.next
curr.next.next = temp
temp.next = temp1
curr = curr.next.next
return dummy_head.next
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, next = head)
prev = dummy
curr = head
while curr and curr.next:
temp1 = curr.next
temp2 = curr.next.next
prev.next = temp1
temp1.next = curr
curr.next = temp2
prev = curr
curr = temp2
return dummy.next
# 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
"""
slow = head
fast = head
while n > 0:
fast=fast.next
n-=1
# commenting out the following two lines will be wrong, since had to deal with case when only one node is in the linked list.
# Thus, for consistent, better using dummy_head
if fast == None and n == 0:
return head.next
while fast.next:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return head
# 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: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy_head = ListNode(0, next = head)
slow = fast = dummy_head
while n >= 0:
fast = fast.next
n -= 1
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy_head.next
如果temp.add(currA.val),那么就是上面所说的问题了,要的是指针不是数值!
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
#solution 1:
# time complexity: O(n+m)
# space complexity: O(n)
temp = set()
currA = headA
currB = headB
while currA:
temp.add(currA)
currA = currA.next
while currB:
if currB in temp:
return currB
currB = currB.next
return None
# 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
"""
curA, curB = headA, headB
#to calculate the length
lenA =0
lenB = 0
while curA:
lenA += 1
curA = curA.next
while curB:
lenB += 1
curB = curB.next
#calculate the difference of both lengths
if lenA > lenB:
diff = lenA - lenB
curL = headA
curS = headB
else:
diff = lenB - lenA
curL = headB
curS = headA
#move the pointer of the longer linked list to the position where has the same subsequent length
while diff > 0 :
curL = curL.next
diff -= 1
# check
while curL:
if curL == curS:
return curL
curL = curL.next
curS = curS.next
return None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
temp = set()
cur = head
while cur:
if cur in temp:
return True
temp.add(cur)
cur = cur.next
return False
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
return True
return False
Q:难道链表不可以有形同的值吗?这样用set()也无法说明是环的起点?
A:注意加入set()的是指针,因此可以用set()来找起点。
# 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
"""
# space complexity: O(n)
temp = set()
curr = head
while curr:
if curr in temp:
return curr
temp.add(curr)
curr = curr.next
return None
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast = slow = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast: #fast and slow meet
#define another two pointers for finding the entrance
index1 = fast #or slow
index2 = head
while index1 != index2:
index1 = index1.next
index2 = index2.next
return index1 #or index2
return None