Niuke.com High Frequency Algorithm Question Series - BM14 - Parity Rearrangement of Linked Lists
Topic description
Given a singly linked list, please set a function to put the odd-numbered nodes and even-numbered nodes of the linked list together, and output them after rearranging.
Note that the number of the node is not the value of the node.See the original title: Parity rearrangement of BM14 linked list
Solution 1: Linked list traversal (using extra space)
First, it is judged that if the linked list is empty or there are only 1 or 2 nodes, no rearrangement is required, and the original linked list is returned directly.
Otherwise, use 2 lists to additionally record nodes with odd and even bits, and the process is as follows:
- Traverse the linked list, and put the node values of odd and even bits into different lists respectively;
- In the order of odd-numbered bits first and even-numbered bits last, recombine the values in the two lists into a new linked list, which is the rearranged linked list, and return it.
Solution 2: Double pointer method
Similarly, first of all, it is necessary to judge that if the linked list is empty or only has 1 or 2 nodes, return the original linked list directly without rearranging.
Otherwise, use the odd and even nodes to point to the first (odd) and second (even) nodes of the linked list respectively, and then traverse the linked list to connect the odd and even nodes respectively, and finally, Put the even-numbered bits behind the odd-numbered linked list, which is the rearranged linked list.
code
import java.util.ArrayList;
import java.util.List;
public class Bm014 {
/**
* 使用额外的空间
*
* @param head ListNode类
* @return ListNode类
*/
public static ListNode oddEvenList(ListNode head) {
// 如果链表为空或者只有1或2个结点,不用重排
if (head == null || head.next == null || head.next.next == null) {
return head;
}
// 奇数结点
List<Integer> odds = new ArrayList<>();
// 偶数结点
List<Integer> evens = new ArrayList<>();
int i = 1;
while (head != null) {
if (i % 2 == 1) {
odds.add(head.val);
} else {
evens.add(head.val);
}
head = head.next;
i++;
}
ListNode newHead = new ListNode(-1), cur = newHead;
for (Integer val : odds) {
cur.next = new ListNode(val);
cur = cur.next;
}
for (Integer val : evens) {
cur.next = new ListNode(val);
cur = cur.next;
}
return newHead.next;
}
/**
* 双指针法
*
* @param head
* @return
*/
public static ListNode oddEvenList2(ListNode head) {
// 如果链表为空或者只有1或2个结点,不用重排
if (head == null || head.next == null || head.next.next == null) {
return head;
}
// 奇数结点
ListNode odd = head;
// 偶数结点,偶数链表头
ListNode evenHead = head.next, even = evenHead;
while (even != null && even.next != null) {
// odd连接奇数位结点
odd.next = even.next;
odd = odd.next;
// even连接偶数位结点
even.next = odd.next;
even = even.next;
}
// 最后将odd链表的最后一个结点指向even链表的表头
odd.next = evenHead;
return head;
}
public static void main(String[] args) {
ListNode head = ListNode.testCase5();
System.out.println("原链表为");
ListNode.print(head);
System.out.println("重排后的链表为");
ListNode.print(oddEvenList(head));
ListNode.print(oddEvenList2(head));
}
}
$1.01^{365} ≈ 37.7834343329$
$0.99^{365} ≈ 0.02551796445$
Believe in the power of persistence!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。