91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

關于鏈表:removeAll()和mergeTwoList()

發布時間:2020-07-22 01:29:06 來源:網絡 閱讀:297 作者:涼白開dream 欄目:編程語言

說明:思路中寫的是代碼,表達基本意思
一、刪除鏈表中所有與val相等的元素
定義兩個結點:prev和cur
遍歷整個鏈表:
相等:prve.next=cur.next
cur=cur.next
prev=prev.next
不相等:cur=cur.next

二、合并兩個有序鏈表
定義兩個結點result(合成的新鏈表的頭結點) last(result的最后一個結點)
如果cur1.val<=cur2.val
last.next=cur1
cur1=cur1.next
否則,同理,更新cur2



```class Node {
    int val;
    Node next = null;

    public Node(int val) {
        this.val = val;
    }
}

public class Solution {
    Node removeAll(Node head, int val) {
        Node prev = null;
        Node cur = head;
        while (cur != null) {
            if (cur.val == val) {
                if (cur == head) {
                    head = cur.next;
                } else {
                    prev.next = cur.next;
                }
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    Node merge(Node head1, Node head2) {
        if (head1 == null) {
            return head2;
        }

        if (head2 == null) {
            return head1;
        }

        Node result = null;
        Node last = null;

        Node cur1 = head1;
        Node cur2 = head2;

        while (cur1 != null && cur2 != null) {
            if (cur1.val <= cur2.val) {
                if (result == null) {
                    result = cur1;
                } else {
                    last.next = cur1;
                }
                last = cur1;
                cur1 = cur1.next;
            } else {
                if (result == null) {
                    result = cur2;
                } else {
                    last.next = cur2;
                }
                last = cur2;
                cur2 = cur2.next;
            }
        }

        if (cur1 != null) {
            last.next = cur1;
        } else {
            last.next = cur2;
        }

        return result;
    }

    public static Node createList() {
        Node n1 = new Node(6);
        Node n3 = new Node(2);
        Node n4 = new Node(6);
        Node n6 = new Node(4);
        Node n8 = new Node(6);

        n1.next = n3;
        n3.next = n4;
        n4.next = n6;
        n6.next = n8;

        return n1;
    }

    public static Node createList1() {
        Node n1 = new Node(1);
        Node n2 = new Node(2);

        n1.next = n2;

        return n1;
    }

    public static Node createList2() {
        Node n1 = new Node(1);
        Node n2 = new Node(3);
        Node n3 = new Node(5);
        Node n4 = new Node(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;

        return n1;
    }

    public static void main(String[] args) {
        Node head = createList();

        Node result = new Solution().removeAll(head, 6);
        for (Node cur = result; cur != null; cur = cur.next) {
            System.out.println(cur.val);
        }

        System.out.println("=====================");

        Node head1 = createList1();
        Node head2 = createList2();
        Node merged = new Solution().merge(head1, head2);//類中的函數返回值是Node類型,用merge接收
        for (Node cur = merged; cur != null; cur = cur.next) {//merge相當于head,代表整個鏈表
            System.out.println(cur.val);
        }
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

中山市| 湖北省| 万全县| 广饶县| 陆良县| 南岸区| 盐源县| 瑞丽市| 章丘市| 嘉兴市| 汽车| 电白县| 苏尼特右旗| 民县| 唐海县| 麻江县| 铜陵市| 攀枝花市| 巨鹿县| 韶山市| 金沙县| 米脂县| 涿鹿县| 正镶白旗| 神农架林区| 庄浪县| 陇川县| 洛扎县| 茌平县| 陵水| 无极县| 杭锦后旗| 龙胜| 三都| 炎陵县| 固安县| 伊宁市| 万州区| 奉新县| 平陆县| 莒南县|