リンク一覧
リンクリストをリバース
単独リンクリストを逆にします。
例:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
ファローアップ:
リンクリストは、いずれかの繰り返しや再帰的に反転させることができます。あなたは両方を実装してもらえますか?
ソリューション:
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
ペアでスワップノード
リンクリストを考えると、すべての2つの隣接ノードを交換し、その頭を返します。
あなたはありませんだけで、それ自体を変更することができるノードは、リストのノードの値を変更します。
例:
Given 1->2->3->4, you should return the list as 2->1->4->3.
ソリューション:
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
リンクリストサイクル
それはそれでサイクルを持っている場合、リンクされたリストを考えると、決定します。
与えられたリンクされたリスト内のサイクルを表すために、我々は整数使用pos
尾部が接続するリンクされたリスト内(0インデックスの)位置を表します。場合pos
で-1
は、リンクされたリストにはサイクルがありません。
例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.
例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.
例3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
ファローアップ:
あなたは使用してそれを解決することができますO(1) (すなわち一定の)メモリを?
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
リンクリストサイクルII
リンクリストを考えると、サイクルが始まるノードを返します。何のサイクルが存在しない場合は、返しますnull
。
与えられたリンクされたリスト内のサイクルを表すために、我々は整数使用pos
尾部が接続するリンクされたリスト内(0インデックスの)位置を表します。場合pos
で-1
は、リンクされたリストにはサイクルがありません。
注意:リンクリストを変更しないでください。
例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.
例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.
例3:
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
フォローアップ:
あなたは余分なスペースを使用せずに、それを解決することはできますか?
ソリューション:
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.
K-グループ内のノードを逆に
リンクリストを考えると、リンクリストのノード逆kは時間にし、その修正のリストを返します。
kは正の整数であり、以下にリンクされたリストの長さに等しいです。ノードの数がの倍数でない場合、K、次いで最終的に左アウトノードはそのまま残るべきです。
例:
このリンクリストを考えます: 1->2->3->4->5
以下のために、K = 2、あなたは返す必要があります:2->1->4->3->5
以下のためのk = 3、あなたは返す必要があります:3->2->1->4->5
注意:
- 唯一の一定の余分なメモリが許可されています。
- あなただけ自体を変更することができるノードは、リストのノードの値を変更しないことがあります。