标签:val change lse its ror speed this space note
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Solutions:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverse_list(self, head: ListNode) -> ListNode:
print(self)
# prev = None
# cur = head
prev, cur = None, head
while cur:
# temp = cur.next
# cur.next = prev
# prev = cur
# cur = temp
cur.next, prev, cur = prev, cur, cur.next
return prev
def reverse_list_II(self, head:ListNode) -> ListNode:
"""Use recursion"""
if not head or not head.next:
return head
N = self.reverse_list_II(head.next)
head.next.next = head
head.next = None
return N
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list‘s nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
Solutions:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def swap_pairs(self, head: ListNode) -> ListNode:
# create a ListNode to record the head
record_node = ListNode(self)
record_node.next = head
# set a cursor
cur = record_node
while cur.next and cur.next.next:
# set two adjoin cursor which attach to the cur cursor
a = cur.next
b = a.next
# change the node.next
"""
cur.next = b
a.next = b.next
b.next = a
"""
cur.next, a.next, b.next = b, b.next, a
# move the cur cursor
cur = a
return record_node.next
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def has_cycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
# set two cursor--a fast && a slow
cur_fast, cur_slow = head, head
while cur_slow and cur_fast:
cur_slow = cur_slow.next
try:
cur_fast = cur_fast.next.next
except AttributeError:
return False
# the fast cursor meet the slow cursor
if cur_fast == cur_slow:
return True
return False
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
Follow-up:
Can you solve it without using extra space?
Solutions:
Consider the following linked list, where E is the cycle entry and X, the crossing point of fast and slow.
H: distance from head to cycle entry E
D: distance from E to X
L: cycle length
fast: crossing 2 nodes each time
slow: crossing 1 node each time
_____
/ head_____H______E \ /
X_____/
If fast and slow both start at head, when fast catches slow, slow has traveled H+D and fast 2(H+D).
Assume fast has traveled n loops in the cycle, we have:
2H + 2D = H + D + nL --> H + D = nL --> H = nL - D
Thus if two pointers start from head and X with same speed, respectively, one first reaches E, the other also reaches E.
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:
标签:val change lse its ror speed this space note
原文地址:https://www.cnblogs.com/pankypan/p/11570330.html