about reverse LinkedList
reverse LinkedList pattern in algorithm, 反转套路
my leetcode
1111
222
Swap Nodes in Pairs 24
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Input: head = [1,2,3,4]
Output: [2,1,4,3]
public ListNode swapPairs(ListNode head) {
if ((head == null)||(head.next == null))
return head;
ListNode n = head.next;
head.next = swapPairs(head.next.next);
n.next = head;
return n;
}
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newHead = head.next;
ListNode prev = new ListNode(0);
while (head != null && head.next != null) {
ListNode next = head.next;
head.next = next.next;
next.next = head;
prev.next = next;
prev = head;
head = head.next;
}
return newHead;
}
Reverse Nodes in k-Group 24
Given the head of a linked list, reverse the nodes of the list k at a time, and return the 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.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
public ListNode reverseKGroup(ListNode head, int k) {
ListNode cur = head;
int count = 0;
while (cur != null && count != k) {
cur = cur.next;
count++;
}
if (count == k) {
cur = reverseKGroup(cur, k);
while (count-- > 0) {
ListNode tmp = head.next;
head.next = cur;
cur = head;
head = tmp;
}
head = cur;
}
return head;
}
public ListNode reverseKGroup(ListNode head, int k) {
int n = 0;
for (ListNode i = head; i != null; n++, i = i.next);
ListNode dummy = new ListNode(0);
dummy.next = head;
for (ListNode prev = dummy, tail = head; n >= k; n -= k) {
for (int i = 1; i < k; i++) {
ListNode next = tail.next.next;
tail.next.next = prev.next;
prev.next = tail.next;
tail.next = next;
}
prev = tail;
tail = tail.next;
}
return dummy.next;
}
Rotate List
Given the head of a linked list, rotate the list to the right by k places.
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
public ListNode rotateRight(ListNode head, int k) {
if (head == null) {
return null;
}
int len = 1;
ListNode fast = head, slow = head;
while (fast.next != null) {
fast = fast.next;
len++;
}
for (int i = 1; i < len - k % len; i++) {
slow = slow.next;
}
fast.next = head;
head = slow.next;
slow.next = null;
return head;
}
Reverse Linked List II 92
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]
method 1:
1 –> 2 , 3 ,4 –> 5 p l r s
. _______________________ . | | 1 2 <– 3 <– 4 5 p l ^ r s |_____________________|
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode prev = new ListNode(1);
prev.next = head;
// prev到了反转的前一个
for (int i = 0; i < left -1; i++) {
prev = prev.next;
}
ListNode rightNode = prev;
// prev 到 right
for (int i = 0; i < right - left + 1; i++) {
rightNode = rightNode.next;
}
// 切除子链接
ListNode leftNode = prev.next;
ListNode curr = rightNode.next;
// 切断连接
pre.next = null;
rightNode.next = null;
// 反转
reverseList(leftNode);
// 连接
pre.next = rightNode;
leftNode.next = curr;
return head;
}
void reverseList(ListNode head) {
ListNode pre = null;
while (head != null) {
ListNode next = head.next;
head.next = pre;
pre = head;
head = next;
}
}
}
没通过[3, 5] 1 这个案例,我一直以为 head 不会动
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(1);
dummy.next = head;
ListNode prev = dummy;
// prev到了反转的前一个
for (int i = 0; i < left -1; i++) {
prev = prev.next;
}
ListNode rightNode = prev;
// prev 到 right
for (int i = 0; i < right - left + 1; i++) {
rightNode = rightNode.next;
}
// 切除子链接
ListNode leftNode = prev.next;
ListNode curr = rightNode.next;
// 切断连接
prev.next = null;
rightNode.next = null;
// 反转
reverseList(leftNode);
// 连接
prev.next = rightNode;
leftNode.next = curr;
return dummy.next;
}
void reverseList(ListNode head) {
ListNode pre = null;
while (head != null) {
ListNode next = head.next;
head.next = pre;
pre = head;
head = next;
}
}
time: ON space: O1
方法一, 找到 left ,right 遍历一次, 反转再遍历一次,如果只遍历一次就用头插法
9 –> 7 –> 2 –> 5 –> 4 –> 3 –> 6
. p c n
把 要反转的都插到 7 后面
9 –> 7 2 <– 5 4 –> 3 –> 6
. p c n ^
. |—–|——^ |
|____________|
- 记录 cur 下一个节点 n
- n 下一个节点指向 p 的下一个节点
- p 下一个节点指向 n
9 –> 7 –> 5 –> 2 –> 4 –> 3 –> 6
.
9 –> 7 –> 4 –> 5 –> 2 –> 3 –> 6
.
9 –> 7 –> 3 –> 4 –> 5 –> 2 –> 6
.
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode dummy = new ListNode(1);
dummy.next = head;
ListNode prev = dummy;
// prev到了反转的前一个
for (int i = 0; i < left -1; i++) {
prev = prev.next;
}
ListNode cur = prev.next;
// prev 到 right
for (int i = 0; i < right - left; i++) {
ListNode next = cur.next;
cur.next = next.next;
next.next = prev.next;
prev.next = next;
}
return dummy.next;
}
Odd Even Linked List 328
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
Input: head = [1,2,3,4,5]
Output: [1,3,5,2,4]
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode odd = head;
ListNode node = head.next;
ListNode even = node;
while (even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = node;
return head;
}
Reverse Nodes in Even Length Groups 2074
You are given the head of a linked list.
The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,
The 1st node is assigned to the first group.
The 2nd and the 3rd nodes are assigned to the second group.
The 4th, 5th, and 6th nodes are assigned to the third group, and so on.
Note that the length of the last group may be less than or equal to 1 + the length of the second to last group.
Reverse the nodes in each group with an even length, and return the head of the modified linked list.
Input: head = [5,2,6,3,9,1,7,3,8,4]
Output: [5,6,2,3,9,1,4,8,3,7]
Explanation:
- The length of the first group is 1, which is odd, hence no reversal occurs.
- The length of the second group is 2, which is even, hence the nodes are reversed.
- The length of the third group is 3, which is odd, hence no reversal occurs.
- The length of the last group is 4, which is even, hence the nodes are reversed.
cur is pointing end of a particular group
start is pointing starting of a particular group
while traversing put node’s values in stack (LIFO)
when the count of nodes in a group is == to the count of nodes required for that group and is even
then move start pointer ahead and update values of nodes in that group.
public ListNode reverseEvenLengthGroups(ListNode head) {
int group = 1;
ListNode cur = head;
while (cur != null) {
int count = 0;
ListNode start = cur;
Deque<Integer> stack = new ArrayDeque<>();
while (count != group && cur != null) {
stack.push(cur.val);
cur = cur.next;
count++;
}
if (count % 2 == 0) {
while (start != cur) {
start.val = stack.pop();
start = start.next;
}
}
group++;
}
return head;
}
A Number After a Double Reversal 2119
Reversing an integer means to reverse all its digits.
For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.
Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.
Example 1:
Input: num = 526
Output: true
Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
public boolean isSameAfterReversals(int num) {
if (num == 0) {
return true;
}
if (num % 10 == 0) {
return false;
} else {
return true;
}
}