链表类——翻转类题目
序号 | 题目 | 难度 | 代码 |
---|---|---|---|
25 | Reverse Nodes in k-Group | Hard | python、java、c++ |
61 | Rotate List | Medium | python、java、c++ |
92 | Reverse Linked List II | Medium | python、java、c++ |
206 | Reverse Linked List | Easy | python、java、c++ |
25. Reverse Nodes in k-Group
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:
- Only constant extra memory is allowed.
- You may not alter the values in the list's nodes, only nodes itself may be changed.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
dummy = jump = ListNode(0)
dummy.next = l = r = head
while True:
cnt = 0
while r and cnt < k:
r = r.next
cnt += 1
if cnt == k:
pre, cur = r, l
for _ in range(k):
cur.next, cur, pre = pre, cur.next,cur
jump.next,jump,l = pre,l,r
else:
return dummy.next
61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
n, pre, current = 0 , None, head
while current:
pre,current = current,current.next
n += 1
if not n or not k % n:
return head
tail = head
for _ in range(n - k % n - 1):
tail = tail.next
next, tail.next, pre.next = tail.next, None, head
return next
92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
dummy = pre = ListNode(0)
dummy.next = head
for _ in range(m-1):
pre = pre.next
cur= pre.next
# reverse the defined part
node = None
for _ in range(n-m+1):
nxt = cur.next
cur.next = node
node = cur
cur= nxt
# connect three parts
pre.next.next = cur
pre.next = node
return dummy.next
206. Reverse Linked List
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?
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
while head:
pre, head.next,head = head,pre,head.next
return pre