链表Dummy Node
(一)Partition List
https://leetcode.com/problems/partition-list/description/
題目:給出一個(gè)鏈表和一個(gè)x值,要求返回一個(gè)順序鏈表使得小于x的數(shù)在鏈表前面,大于等于x的數(shù)在鏈表后面,保證節(jié)點(diǎn)順序不變。
? ? ? ? ? 例如:1->3->2->4->2, 3 變成:1->2->2->3->4
解答:建立兩個(gè)新的左、右指針及dummy node,使用head指針遍歷整個(gè)鏈表,遇到大于等于head的節(jié)點(diǎn)則放到右鏈表,否則放到左鏈表。最后將左右鏈表相連。
第一次犯錯(cuò):忘記將又指針的尾部指向null;
代碼:
class Solution {
? ? public ListNode partition(ListNode head, int x) {
? ? ? ? ?if (head == null) {
? ? ? ? ? ? return head;
? ? ? ? }
? ? ? ??
? ? ? ? ListNode leftDummy = new ListNode(0);
? ? ? ? ListNode rightDummy = new ListNode(0);
? ? ? ? ListNode left = leftDummy;
? ? ? ? ListNode right = rightDummy;
? ? ? ??
? ? ? ? while (head != null) {
? ? ? ? ? ? if (head.val >= x) {
? ? ? ? ? ? ? ? right.next = head;
? ? ? ? ? ? ? ? right = right.next;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? left.next = head;
? ? ? ? ? ? ? ? left = left.next;
? ? ? ? ? ? }
? ? ? ? ? ? head = head.next;
? ? ? ? }
? ? ? ? right.next = null;
? ? ? ? left.next = rightDummy.next;
? ? ? ? return leftDummy.next;
? ? }
}
(二)?Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/description/
AC!
題目:將兩個(gè)順序鏈表合并成一個(gè)順序鏈表;
解答:依次比較兩個(gè)鏈表里的值大小進(jìn)行排列;
改進(jìn):當(dāng)一個(gè)鏈表指針指向null,另一個(gè)鏈表還沒(méi)時(shí),可以直接將重新排列的鏈表尾指向當(dāng)前指針:
? ? ? ? ? ?if (l1 != null) {
? ? ? ? ? ?head.next = l1;
? ? ? ? ? ?} else {
? ? ? ? ? ?head.next = l2;
? ? ? ? ? }
代碼:
class Solution {
? ? public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
? ? ? ? ListNode dummy = new ListNode(0);
? ? ? ? ListNode head = dummy;
? ? ? ??
? ? ? ? while (l2 != null && l1 != null) {
? ? ? ? ? ? if (l1.val <= l2.val) {
? ? ? ? ? ? ? ? head.next = l1;
? ? ? ? ? ? ? ? l1 = l1.next;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? head.next = l2;
? ? ? ? ? ? ? ? l2 = l2.next;
? ? ? ? ? ? }
? ? ? ? ? ? head = head.next;
? ? ? ? }?
? ? ? ? while (l1 != null) {
? ? ? ? ? ? head.next = l1;
? ? ? ? ? ? l1 = l1.next;
? ? ? ? ? ? head = head.next;
? ? ? ? }
? ? ? ? while (l2 != null) {
? ? ? ? ? ? head.next = l2;
? ? ? ? ? ? l2 = l2.next;
? ? ? ? ? ? head = head.next;
? ? ? ? }
? ? ? ? head.next = null;
? ? ? ? return dummy.next;
? ? }
}
(三)swap two nodes in linked list
https://leetcode.com/problems/swap-nodes-in-pairs/description/
AC!
題目:兩兩交換鏈表中節(jié)點(diǎn)位置。如:1->2->4->5->6 轉(zhuǎn)變?yōu)? 2->1->5->4->6
解答:使用兩個(gè)指針遍歷鏈表;
代碼:
class Solution {
? ? public ListNode swapPairs(ListNode head) {
? ? ? ? if (head == null) {
? ? ? ? ? ? return head;
? ? ? ? }
? ? ? ??
? ? ? ? ListNode dummy = new ListNode(0);
? ? ? ? dummy.next = head;
? ? ? ? ListNode headNext = head.next;
? ? ? ? ListNode headPrev = dummy;
? ? ? ??
? ? ? ? while (head != null && headNext != null) {
? ? ? ? ? ? headPrev.next = headNext;
? ? ? ? ? ? head.next = headNext.next;
? ? ? ? ? ? headNext.next = head;
? ? ? ? ? ? headPrev = head;
? ? ? ? ? ? head = head.next;
? ? ? ? ? ? if (head != null) {
? ? ? ? ? ? ? ? headNext = head.next;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return dummy.next;
? ? }
}
(四)reorder list
https://leetcode.com/problems/reorder-list/description/
題目:給定一個(gè)單鏈表:L0->L1->...->Ln-1->Ln,
? ? ? ? ? 重新排序后為:L0->Ln->L1->Ln-1->L2->Ln-2->...
解答:
第一次犯錯(cuò):將鏈表尾連接到頭之后,忘記將尾的前一個(gè)指針指向空;
第二次犯錯(cuò):忘記考慮鏈表頭和prevTail重合的情況(偶數(shù)鏈表)。
代碼:
class Solution {
? ? public void reorderList(ListNode head) {
? ? ? ? while (head != null && head.next != null) {
? ? ? ? ? ? ListNode prevTail = head;
? ? ? ? ? ? while (prevTail.next.next != null) {
? ? ? ? ? ? ? ? prevTail = prevTail.next;
? ? ? ? ? ? }
? ? ? ? ? ? ListNode tail = prevTail.next;
? ? ? ? ? ? if (head.next == tail) {
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }?
? ? ? ? ? ? tail.next = head.next;
? ? ? ? ? ? head.next = tail;
? ? ? ? ? ? head = head.next.next;
? ? ? ? ? ? prevTail.next = null;
? ? ? ? }
? ? }
}
(四)Rotate List
https://leetcode.com/problems/rotate-list/description/
題目:將鏈表尾部的k個(gè)節(jié)點(diǎn)移到鏈表頭部;
解答:每次將鏈表最后一個(gè)節(jié)點(diǎn)移動(dòng)至鏈表頭,移動(dòng)k次;
第一次犯錯(cuò):(超時(shí))先遍歷鏈表,得到鏈表長(zhǎng)度length,循環(huán)只需執(zhí)行 k%length 次;
代碼:
class Solution {
? ? public ListNode rotateRight(ListNode head, int k) {
? ? ? ? ListNode tail = head;
? ? ? ? int length = 0;
? ? ? ??
? ? ? ? while (tail != null ) {
? ? ? ? ? ? length++;
? ? ? ? ? ? tail = tail.next;
? ? ? ? }
? ? ? ??
? ? ? ? if (length == 0 || length == 1) {
? ? ? ? ? ? return head;
? ? ? ? }
? ? ? ??
? ? ? ? for (int i = 0; i < k % length; i++) {
? ? ? ? ? ? ListNode preTail = head;
? ? ? ? ? ? while (preTail != null && preTail.next != null && preTail.next.next != null) {
? ? ? ? ? ? ? ? preTail = preTail.next;
? ? ? ? ? ? }
? ? ? ? ? ? tail = preTail.next;
? ? ? ? ? ? tail.next = head;
? ? ? ? ? ? preTail.next = null;
? ? ? ? ? ? head = tail;
? ? ? ? }
? ? ? ? return head;
? ? }
}
總結(jié)
以上是生活随笔為你收集整理的链表Dummy Node的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 正则表达式匹配中文及符号、英文及符号数字
- 下一篇: 文件关联修复命令ftype用法