Da04 | 24. 两两交换链表中的节点,19.删除链表的倒数第N个节,面试题 02.07. 链表相交,142.环形链表II

24 两两交换链表中的节点

链接:LeetCode 24

public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode cur = dummy;
            while(cur.next != null && cur.next.next != null) {
                // ListNode tmp = cur.next.next.next;
                ListNode first = cur.next;
                ListNode second = cur.next.next;

                first.next = second.next;
                second.next = first;
                cur.next = second;
                cur = first;
            }

            return dummy.next;
    }

19 删除链表的倒数第N个节

链接:LeetCode 19
思路:采取快慢节点,可以达到 O(N) 的时间复杂度

    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = head;
        ListNode slow = dummy;
        for(int i = 0; i < n - 1; i++) {
            fast = fast.next;
        }
    
        while(fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }

面试题 02.07. 链表相交

链接:面试题 02.07. 链表相交
思路:采取快慢节点,可以达到 O(N) 的时间复杂度

 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        
        if (headA == null || headB == null) {
            return null;
        }
        
        ListNode cura = headA;
        ListNode curb = headB;
        int lena = 0;
        int lenb = 0;
        while(cura != null) {
            cura = cura.next;
            lena++;
        }
        while(curb != null) {
            curb = curb.next;
            lenb++;
        }

        cura = headA;
        curb = headB;
        if(lenb > lena) {
            int temp = lena;
            lena = lenb;
            lenb = temp;
            ListNode tmpNode = cura;
            cura = curb;
            curb = tmpNode;
        }
        int gap = lena - lenb;
        while(gap-- > 0) {
            cura = cura.next;
        }

        while(cura != null) {
            if (cura == curb) {
                return cura;
            }
            cura = cura.next;
            curb = curb.next;
        }
        return null;
    }

142.环形链表II

链接:LeetCode 142
思路:算法实现不难,主要是数学的推导过程

    public ListNode detectCycle(ListNode head) {
          ListNode slow = head;
          ListNode fast = head;
          if (head == null) {
              return null;
          }
          while(fast.next != null && fast.next.next != null) {
              // 移动放在前面,因为一开始都在 head 节点
              slow = slow.next;
              fast = fast.next.next;
              if(slow == fast) {
                ListNode res = head;
                while (res != slow) {
                    res = res.next;
                    slow = slow.next;
                }
                return res;
              }
          }
          return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值