Java源码,链表的所有操作

包括了生成一条链表,以及合并两个有序链表,反转链表,两个链表的公共节点,倒数第k个节点,含有环的链表的中环的入口节点,删除链表中重复的元素(经测试是可以直接食用的)

package list;

import java.util.ArrayList;

public class ListBase {
    static class ListNode {
        private ListNode next;
        private int val;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
            this.next = null;
        }
    }

    /**
     * 生成一条长度为n的链表
     *
     * @param n
     * @return
     */
    public ListNode createNNodeList(int n) {
        ListNode temp = new ListNode();
        ListNode head = null;
        for (int i = 0; i < n; i++) {
            ListNode newNode = new ListNode(i);
            if (null == head) {
                head = temp = newNode;
            } else {
                temp.next = newNode;
                temp = newNode;
            }
        }
        return head;
    }

    /**
     * 传入一个数组生成一条链表
     * @param array
     * @return
     */
    public ListNode createNNodeList(int[] array){
        if(null == array){
            return null;
        }
        int n = array.length;
        ListNode head = null, cur = null;
        for (int i = 0; i < n; i++) {
            ListNode temp = new ListNode(array[i]);
            if(null == head){
                head = cur = temp;
            }else{
                cur.next = temp;
                cur = temp;
            }
        }
        return head;
    }
    /**
     * 打印链表
     *
     * @param head
     */
    public static void dispListNode(ListNode head) {
        ListNode temp = head;
        while (null != temp) {
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    /**
     * 合并两个有序链表,非递归的做法
     *
     * @param head1
     * @param head2
     * @return
     */
    public ListNode megerListNonRecursive(ListNode head1, ListNode head2) {
        if (null == head1) {
            return head2;
        } else if (null == head2) {
            return head1;
        }
        ListNode pNode = null, result = null;
        while (null != head1 && null != head2) {
            if (head1.val <= head2.val) {
                if (null == result) {
                    pNode = result = head1;
                } else {
                    pNode.next = head1;
                    pNode = pNode.next;
                }
                head1 = head1.next;
            } else {
                if (null == result) {
                    result = pNode = head2;
                } else {
                    pNode.next = head2;
                    pNode = pNode.next;
                }
                head2 = head2.next;
            }
        }
        if (null != head1) {
            pNode.next = head1;
        } else if (null != head2) {
            pNode.next = head2;
        }
        return result;
    }

    /**
     * 使用递归版本的合并两个链表
     *
     * @param head1
     * @param head2
     * @return
     */
    public ListNode megerListRecursive(ListNode head1, ListNode head2) {
        if (null == head1) {
            return head2;
        }
        if (null == head2) {
            return head1;
        }
        if (head1.val <= head2.val) {
            head1.next = megerListRecursive(head1.next, head2);
            return head1;
        } else {
            head2.next = megerListRecursive(head1, head2.next);
            return head2;
        }
    }

    /**
     * 反转链表
     *
     * @param head
     * @return
     */
    public ListNode reseverList(ListNode head) {
        if (null == head || null == head.next) {
            return head;
        }
        ListNode cur = head, pre = null, res = null, temp = null;
        while (null != cur) {
            temp = cur.next;
            if (null == temp) {
                res = cur;
            }
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return res;
    }

    /**
     * 两个链表的公共节点
     *
     * @param head1
     * @param head2
     * @return
     */
    public ListNode sameListNode(ListNode head1, ListNode head2) {
        //假设他们之间的公共部分是a的长度,然后非公共部分一个为b,一个为c,那么,只要一起走过a+b+c的路程就肯定是第一个节点
        ListNode p1 = head1, p2 = head2;
        while (p1 != p2) {
            p1 = (p1 == null) ? head2 : p1.next;
            p2 = (p2 == null) ? head1 : p2.next;
        }
        return p1;
    }

    /**
     * 返沪倒数第k个节点
     *
     * @param head
     * @param k
     * @return
     */
    public ListNode findKThNode(ListNode head, int k) {
        //判断边界条件
        if (k < 0 || null == head) {
            return null;
        }
        ListNode cur = head, res = head;
        //先让当前节点向前走k个节点,之后在一直走到末尾即可得到倒数第k个节点
        for (int i = 0; i < k; i++) {
            if (null == cur) {
                return null;
            }
            cur = cur.next;
        }
        while (null != cur) {
            cur = cur.next;
            res = res.next;
        }
        return res;
    }

    /**
     * 含有环的链表中环的入口节点
     *
     * @param head
     * @return
     */
    public ListNode entryNodeOfLoopList(ListNode head) {

        //使用快慢指针来做,首先,使用一个节点速度为1,另外一个节点的速度为2,那么,如果有环他们肯定在环的某一个点会相遇
        //其次,可以通过证明推导得知,从快慢指针相遇点到环入口和从开始节点到环入口肯定是会相遇的
        ListNode fast = head, low = head;
        while (null != fast && null != fast.next) {
            fast = fast.next.next;
            low = low.next;
            if (low == fast) {
                break;
            }
        }
        low = head;
        while (low != fast) {
            low = low.next;
            fast = fast.next;
        }
        return low;
    }

    /**
     * 删除链表中重复的连续的元素
     *
     * @param head
     * @return
     */
    public ListNode deleteDuplicate(ListNode head) {
        if (null == head || null == head.next) {
            return head;
        }
        ListNode pHead = new ListNode(-1);
        ListNode pre = pHead, cur = pHead.next;
        while (null != cur) {
            if (null != cur.next && cur.val == cur.next.val) {
                while (null != cur.next && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = pre.next;
                cur = cur.next;
            }
        }
        return pHead.next;
    }

    //方便测试
    public static void main(String[] args) {
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值