链表刷题总结

重刷链表,做一个链表的总结,自用。

技巧篇

快慢指针的应用,环形链表和找中间节点,平移链表。

处理头节点的小技巧,设置一个dummy结点

hashSet可以用来找相同结点,环形结点。

递归/迭代解题。

经典的链表题

链表排序,环形链表,合并链表,反转链表。

巧妙用到了多个题解结合,题解参考力扣官方题解:

143. 重排链表 找到中间节点+反转后半段+合并链表

    public void reorderList(ListNode head) {
        ListNode middle = middle(head);
        ListNode l2 = reverseList(middle.next);
        ListNode l1 = head;
        middle.next = null;
        mergeList(l1, l2);     
        
    }
    //反转链表, 递归
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
    //快慢指针
    public ListNode middle(ListNode head){
        ListNode fast = head;
        ListNode slow = head;
        while(fast!= null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    public void mergeList(ListNode l1, ListNode l2){
        while(l1 != null && l2 != null){
            ListNode l1_tmp = l1.next;
            ListNode l2_tmp = l2.next;
            l1.next = l2;
            l2.next = l1_tmp;
            l1 = l1_tmp;
            l2 = l2_tmp;
        }
    }

148. 排序链表自顶向下,先找到中点(快慢指针),然后递归排序左半边和右半边,最后合并结果。

    public ListNode sortList(ListNode head) {
        return sortList(head, null);
    }

	//递归,自顶向下合并
    public ListNode sortList(ListNode head, ListNode tail){
        if(head == tail){
            return head;
        }
        //边界
        if(head.next == tail){
            head.next = null;
            return head;
        }
        ListNode mid = middle(head, tail);
        ListNode res = merge(sortList(head, mid), sortList(mid, tail));//范围
        return res;
    }

    //合并有序链表
    public ListNode merge(ListNode list1, ListNode list2){
        ListNode dummy = new ListNode(-1);
        ListNode pre = dummy;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                pre.next = list1;
                list1 = list1.next;
            }else{
                pre.next = list2;
                list2 = list2.next;
            }
            pre = pre.next;
        }
        pre.next = list1 == null? list2 : list1;
        return dummy.next;
    }

    // 快慢指针
    public ListNode middle(ListNode head, ListNode tail){
        ListNode fast = head, slow = head;
        while(fast != tail){
            fast = fast.next;
            slow = slow.next;
            //空指针异常注意
            if(fast != tail){
                fast = fast.next;
            }
            
        }
        return slow;
    }

23. 合并K个升序链表类似上一题。

25. K 个一组翻转链表

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode hair = new ListNode(0);
        hair.next = head;
        ListNode pre = hair;

        while(head != null){           
            ListNode tail = pre;
            for(int i = 0; i < k; i++){
                tail = tail.next;
                if(tail == null){
                    return hair.next;
                }
            }
            ListNode nex = tail.next;
            ListNode[] reverse = reverse(head, tail);
            head = reverse[0];
            tail = reverse[1];
            //接回去
            pre.next = head;
            tail.next = nex;
            pre = tail;
            head = nex;
        }
        return hair.next;
    }

    public ListNode[] reverse(ListNode head, ListNode tail){
        ListNode pre = tail.next;
        ListNode curr = head;
        while(pre != tail){
            ListNode temp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = temp;
        }
        return new ListNode[]{tail, head};
    }

206. 反转链表

   //递归
	public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }
	//迭代
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值