亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

[LintCode] Swap Two Nodes in Linked List

wua_wua2012 / 1513人閱讀

摘要:建立結(jié)點(diǎn),指向可能要對(duì)進(jìn)行操作。找到值為和的結(jié)點(diǎn)設(shè)為,的前結(jié)點(diǎn)若和其中之一為,則和其中之一也一定為,返回頭結(jié)點(diǎn)即可。正式建立,,以及對(duì)應(yīng)的結(jié)點(diǎn),,然后先分析和是相鄰結(jié)點(diǎn)的兩種情況是的前結(jié)點(diǎn),或是的前結(jié)點(diǎn)再分析非相鄰結(jié)點(diǎn)的一般情況。

Problem

Given a linked list and two values v1 and v2. Swap the two nodes in the linked list with values v1 and v2. It"s guaranteed there is no duplicate values in the linked list. If v1 or v2 does not exist in the given linked list, do nothing.

Notice

You should swap the two nodes with values v1 and v2. Do not directly swap the values of the two nodes.

Example

Given 1->2->3->4->null and v1 = 2, v2 = 4.

Return 1->4->3->2->null.

Note

建立dummy結(jié)點(diǎn),指向head(可能要對(duì)head進(jìn)行操作)。
找到值為v1和v2的結(jié)點(diǎn)(設(shè)為n1,n2)的前結(jié)點(diǎn)p1, p2;
若p1和p2其中之一為null,則n1和n2其中之一也一定為null,返回頭結(jié)點(diǎn)即可。
正式建立n1,n2,以及對(duì)應(yīng)的next結(jié)點(diǎn)x1,x2,然后:
先分析n1和n2是相鄰結(jié)點(diǎn)的兩種情況:n1是n2的前結(jié)點(diǎn),或n2是n1的前結(jié)點(diǎn);
再分析非相鄰結(jié)點(diǎn)的一般情況。
返回dummy.next,結(jié)束。

Solution
public class Solution {
    public ListNode swapNodes(ListNode head, int v1, int v2) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode p1 = null, p2 = null, cur = dummy;
        while (cur.next != null) {
            if (cur.next.val == v1) p1 = cur;
            else if (cur.next.val == v2) p2 = cur;
            cur = cur.next;
        }
        if (p1 == null || p2 == null) return dummy.next;
        ListNode n1 = p1.next, n2 = p2.next, x1 = n1.next, x2 = n2.next;
        if (p1.next == p2) {
            p1.next = n2;
            n2.next = n1;
            n1.next = x2;
        }
        else if (p2.next == p1) {
            p2.next = n1;
            n1.next = n2;
            n2.next = x1;
        }
        else {
            p1.next = n2;
            n2.next = x1;
            p2.next = n1;
            n1.next = x2;
        }
        return dummy.next;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65154.html

相關(guān)文章

  • [LintCode] Swap Nodes in Pairs

    摘要:指針為,我們選擇的兩個(gè)結(jié)點(diǎn)是和。要注意循環(huán)的邊界條件,這兩個(gè)結(jié)點(diǎn)不能為空。主要思路是先用和兩個(gè)新結(jié)點(diǎn)去保存和兩個(gè)結(jié)點(diǎn)。完成交換之后,連接和,并讓前進(jìn)至此時(shí)的結(jié)點(diǎn)。 Problem Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you sh...

    EscapedDog 評(píng)論0 收藏0
  • [LeetCode/LintCode] Odd Even Linked List

    Problem Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. Example Example:Given...

    awokezhou 評(píng)論0 收藏0
  • [Leetcode] Swap Nodes in Pairs Reverse Nodes in k-

    摘要:三指針?lè)◤?fù)雜度時(shí)間空間思路基本的操作鏈表,見(jiàn)注釋。注意使用頭節(jié)點(diǎn)方便操作頭節(jié)點(diǎn)。翻轉(zhuǎn)后,開(kāi)頭節(jié)點(diǎn)就成了最后一個(gè)節(jié)點(diǎn)。 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should ...

    TZLLOG 評(píng)論0 收藏0
  • [LintCode] Reverse Nodes in k-Group

    Problem Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as ...

    XGBCCC 評(píng)論0 收藏0
  • [LintCode/LeetCode] Partition List

    摘要:新建兩個(gè)鏈表,分別存和的結(jié)點(diǎn)。令頭結(jié)點(diǎn)分別叫作和,對(duì)應(yīng)的指針?lè)謩e叫作和。然后遍歷,當(dāng)小于的時(shí)候放入,否則放入。最后,讓較小值鏈表尾結(jié)點(diǎn)指向較大值鏈表頭結(jié)點(diǎn),再讓較大值鏈表尾結(jié)點(diǎn)指向。 Problem Given a linked list and a value x, partition it such that all nodes less than x come before no...

    崔曉明 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

wua_wua2012

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<