链表(中)

public class Demo01 {

public ListNode removeElements(ListNode head, int val) {
    // 1. 链表为空的情况
    if(head == null) {
        return null;
    }
    // 2. 处理非头结点
    ListNode prev = head;
    ListNode node = head.next;
    while(node != null) {
        if(node.val == val) {
            prev.next = node.next;
            node = prev.next;
        }else {
            prev = node;
            node = node.next;
        }

    }
    // 3. 处理头结点
    if(head.val == val) {
        head = head.next;
    }
    return head;
}

public ListNode reverseList(ListNode head) {
    // 1. 判定空链表的情况
    if (head == null) {
        return null;
    }
    // 2. 只有一个元素
    if (head.next == null) {
        return head;
    }
    ListNode newHead = null;
    // 3. 处理一般的情况
    ListNode cur = head;
    ListNode prev = null;
    while(cur != null){
        ListNode next = cur.next;
        if (next == null) {
            // cur 已经指向最后一个节点了
            newHead = cur;
        }
        cur.next = prev;
        prev = cur;
        cur = next;
    }
    return newHead;
}

public ListNode middleNode(ListNode head) {
    ListNode cur = head;
    int steps = size(head) / 2;
    for (int i = 0; i < steps; i++) {
        cur = cur.next;
    }
    return cur;
}

public ListNode FindKthToTail(ListNode head,int k) {
    int len = size(head);
    if(head == null || k <= 0 || k > len){
        return null;
    }
    int offset = len - k;
    ListNode cur = head;
    for (int i = 0; i < offset; i++) {
        cur = cur.next;
    }
    return cur;
}

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if (l1 == null) {
        return l2;
    }
    if (l2 == null) {
        return l1;
    }
    ListNode cur1 = l1;
    ListNode cur2 = l2;
    ListNode newHead = null;
    ListNode newTail = null;
    while (cur1 != null && cur2 != null) {
        if (cur1.val < cur2.val) {
            // 就把 cur1 指向的节点插入到新链表的尾部
            if (newHead == null) {
                // 新链表是空链表
                newHead = cur1;
                newTail = cur1;
                cur1 = cur1.next;
            } else {
                // 新链表不是空链表
                newTail.next = cur1;
                // 更新尾部的指向.
                newTail = newTail.next;
                cur1 = cur1.next;
            }
        } else {
            // 就把 cur2 指向的节点插入到新链表的尾部
            if (newHead == null) {
                newHead = cur2;
                newTail = cur2;
                cur2 = cur2.next;
            } else {
                newTail.next = cur2;
                newTail = newTail.next;
                cur2 = cur2.next;
            }
        }
    }
    // 如何判定当前哪个链表到达结尾, 哪个链表还有剩余?
    if (cur1 == null) {
        // cur2 还有剩余
        newTail.next = cur2;
    } else {
        // cur1 还有剩余
        newTail.next = cur1;
    }
    return newHead;
}


public ListNode partition(ListNode pHead, int x) {
    if (pHead == null) {
        return null;
    }
    if (pHead.next == null) {
        // 只有一个元素
        return pHead;
    }
    ListNode smallHead = new ListNode(-1);
    ListNode smallTail = smallHead;
    ListNode bigHead = new ListNode(-1);
    ListNode bigTail = bigHead;
    for (ListNode cur = pHead;
         cur != null; cur = cur.next) {
        if (cur.val < x) {
            // 插入到 smallTail 之后
            smallTail.next = new ListNode(cur.val);
            smallTail = smallTail.next;
        } else {
            // 插入到 bigTail 之后
            bigTail.next = new ListNode(cur.val);
            bigTail = bigTail.next;
        }
    }
    smallTail.next = bigHead.next;
    return smallHead.next;
}

public ListNode deleteDuplication(ListNode pHead) {
    // 创建一个新的链表, 用来放置不重复的元素
    ListNode newHead = new ListNode(-1);
    ListNode newTail = newHead;

    ListNode cur = pHead;
    while (cur != null) {
        if (cur.next != null
                && cur.val == cur.next.val) {
            // 说明 cur 指向的位置已经是重复的节点了
            // 继续往后找 cur, 找到那个不重复的节点的位置
            // 这样做是为了把若干个相同的节点都跳过去
            while (cur.next != null
                    && cur.val == cur.next.val) {
                cur = cur.next;
            }
            // 循环结束, cur 指向的是这片重复元素的最后一个
            // 再多走一步, cur 指向的就是不重复的元素了
            cur = cur.next;
        } else {
            // 当前这个节点不是重复节点
            // 就把这个节点插入到新链表中
            newTail.next = new ListNode(cur.val);
            newTail = newTail.next;
            cur = cur.next;
        }
    }   // end while
    return newHead.next;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值