This article introduces the problem of linked list in LeetCode problem set.
Other LeetCode questions about linked lists:
LeetCode Problem Set: Linked List (1)
LeetCode Problem Set: Linked List (2)
19. Remove Nth Node From End of List (delete the last N node of the linked list)
Problem Description
ideas and code
There are two solutions to this problem.
First, you can first traverse the list to determine the length, and then do another traverse to delete the Nth node from the bottom.
code show as below:
# 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: ListNode, n: int) -> ListNode:
dummy = ListNode(val=-1, next=head)
num_node = 0 # number of nodes
while head:
num_node += 1
head = head.next
res = dummy
head = dummy
while head.next:
if num_node == n:
head.next = head.next.next
break
else:
num_node -= 1
head = head.next
res = res.next
return res
running result:
Second, the fast and slow pointer method, first move the fast pointer for N steps, and then move the fast and slow pointers at the same time. When the fast pointer moves to the end, the slow pointer is at the bottom N+1 node, and its next point points to the bottom N- 1 node (next next node) is enough.
code show as below:
# 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: ListNode, n: int) -> ListNode:
fast, slow = head, head
# move fast n steps in advance
for i in range(n):
fast = fast.next
# special case: remove the 1st node
if not fast:
return head.next
# move fast and slow together
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next # remove the nth node from the end
return head
running result:
203. Remove Linked List Elements
Problem Description
ideas and code
The idea of this question is simple, traverse nodes one by one, and modify the next parameter of the previous node for the node that needs to be deleted.
code show as below:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(val=-1, next=head)
head = dummy
while head.next:
if head.next.val == val:
head.next = head.next.next
else:
head = head.next
res = dummy.next
return res
running result:
61. Rotate List
Problem Description
ideas and code
The idea of this question is to observe the result of the rotation of the linked list. It can be seen that the rotation is equivalent to cutting the linked list into two segments and then rejoining them. The cutting position can be determined by the number of rotations k, and then modify the next parameter of the corresponding node.
Note two special cases:
- When the linked list is empty, directly return to the original empty linked list
- Calculate the remainder of the number of rotations k to the length of the linked list, if the result is 0, return to the original linked list
code show as below:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return head
dummy = ListNode(val=-1, next=head)
num_node = 0
head = dummy
while head.next:
num_node += 1
head = head.next
# in case: k > num_node
k %= num_node
if not k:
return dummy.next
head_ori = dummy.next # original head
head = dummy.next
for _ in range(num_node - k):
head = head.next
dummy.next = head # new head
for _ in range(k - 1):
head = head.next
head.next = head_ori # next node of tail -> original head
for _ in range(num_node - k):
head = head.next
head.next = None
return dummy.next
running result:
The official problem solution gives another way of thinking. First, the linked list is formed into a ring, and then the corresponding node position is found according to the number of rotations k to solve the ring.
code show as below:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next:
return head
n = 1 # number of nodes
cur = head
while cur.next:
cur = cur.next
n += 1
k %= n # k < n
if not k:
return head
cur.next = head
for _ in range(n - k):
cur = cur.next
res = cur.next
cur.next = None # break the loop
return res
running result: