- 移除串列元素 (Remove Linked List Elements) - 題號:203
題目描述:刪除串列中等於給定值 val 的所有節點。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def removeElements(head, val):
dummy = ListNode(0)
dummy.next = head
prev, curr = dummy, head
while curr:
if curr.val == val:
prev.next = curr.next
else:
prev = curr
curr = curr.next
return dummy.next
- 設計串列 (Design Linked List) - 題號:707
題目描述:設計串列的實現。你可以選擇使用單串列或雙串列。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
class MyLinkedList:
def __init__(self):
self.head = None
def get(self, index):
if index < 0 or not self.head:
return -1
curr = self.head
for _ in range(index):
if not curr.next:
return -1
curr = curr.next
return curr.val
def addAtHead(self, val):
new_head = ListNode(val)
new_head.next = self.head
self.head = new_head
def addAtTail(self, val):
if not self.head:
self.head = ListNode(val)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = ListNode(val)
def addAtIndex(self, index, val):
if index < 0:
self.addAtHead(val)
return
curr = self.head
for _ in range(index - 1):
if not curr:
return
curr = curr.next
if not curr:
return
new_node = ListNode(val)
new_node.next = curr.next
curr.next = new_node
def deleteAtIndex(self, index):
if index < 0:
return
if index == 0:
self.head = self.head.next
return
curr = self.head
for _ in range(index - 1):
if not curr:
return
curr = curr.next
if not curr or not curr.next:
return
curr.next = curr.next.next
- 翻轉鏈表 (Reverse Linked List) - 題號:206
題目描述:反轉一個單鏈表。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def reverseList(head):
prev = None
while head:
next_node = head.next
head.next = prev
prev = head
head = next_node
return prev
- 兩兩交換串列中的節點 (Swap Nodes in Pairs) - 題號:24
題目描述:給定一個串列,兩兩交換其中相鄰的節點,並返回交換後的串列。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def swapPairs(head):
dummy = ListNode(0)
dummy.next = head
prev = dummy
while head and head.next:
first = head
second = head.next
prev.next = second
first.next = second.next
second.next = first
prev = first
head = first.next
return dummy.next
- 刪除串列的倒數第 N 個結點 (Remove Nth Node From End of List) - 題號:19
題目描述:給定一個鏈表,刪除串列的倒數第 n 個節點,並且返回串列的頭結點。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def removeNthFromEnd(head, n):
dummy = ListNode(0)
dummy.next = head
slow = fast = dummy
for _ in range(n + 1):
fast = fast.next
while fast:
slow = slow.next
fast = fast.next
slow.next = slow.next.next
return dummy.next
- 串列相交
題目描述:給定兩個單串列,判斷它們是否相交,並返回相交的起始節點。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def getIntersectionNode(headA, headB):
if not headA or not headB:
return None
p1 = headA
p2 = headB
while p1 != p2:
p1 = headB if p1 is None else p1.next
p2 = headA if p2 is None else p2.next
return p1
- 環形串列 (Linked List Cycle) - 題號:142
題目描述:給定一個串列,判斷串列中是否有環。
class ListNode:
def __init__(self, val=0):
self.val = val
self.next = None
def hasCycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False