关于链表:removeAll()和mergeTwoList()-创新互联

说明:思路中写的是代码,表达基本意思
一、删除链表中所有与val相等的元素
定义两个结点:prev和cur
遍历整个链表:
相等:prve.next=cur.next
cur=cur.next
prev=prev.next
不相等:cur=cur.next

创新互联-专业网站定制、快速模板网站建设、高性价比合肥网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式合肥网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖合肥地区。费用合理售后完善,10年实体公司更值得信赖。

二、合并两个有序链表
定义两个结点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);
        }
    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享名称:关于链表:removeAll()和mergeTwoList()-创新互联
文章源于:http://azwzsj.com/article/picjo.html