leetcode

代码随想录计划

Day 04

两两交换链表中的节点

两两交换链表中的节点

主要还是要想好交换的过程,考虑清楚交换过程就会清楚需要几个引用变量,剩下的就是边界的判断。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null) return head;
        ListNode newHead = new ListNode(-1,head);
        ListNode left = head;
        ListNode right = head.next;
        ListNode pre = newHead;

        while(left != null && right !=  null){
            left.next = right.next;
            right.next = pre.next;
            pre.next = right;
            if(left.next != null){
                right = left.next.next;
            }
            pre = left;
            left = left.next;
        }
        return newHead.next;
    }
}

示例代码:

递归版还是有点难想

// 递归版本
class Solution {
    public ListNode swapPairs(ListNode head) {
        // base case 退出提交
        if(head == null || head.next == null) return head;
        // 获取当前节点的下一个节点
        ListNode next = head.next;
        // 进行递归
        ListNode newNode = swapPairs(next.next);
        // 这里进行交换
        next.next = head;
        head.next = newNode;

        return next;
    }
} 
class Solution {
  public ListNode swapPairs(ListNode head) {
        ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点
        dumyhead.next = head; // 将虚拟头结点指向head,这样方便后面做删除操作
        ListNode cur = dumyhead;
        ListNode temp; // 临时节点,保存两个节点后面的节点
        ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点
        ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点
        while (cur.next != null && cur.next.next != null) {
            temp = cur.next.next.next;
            firstnode = cur.next;
            secondnode = cur.next.next;
            cur.next = secondnode;       // 步骤一
            secondnode.next = firstnode; // 步骤二
            firstnode.next = temp;      // 步骤三
            cur = firstnode; // cur移动,准备下一轮交换
        }
        return dumyhead.next;  
    }
}
删除链表的倒数第N个结点

删除链表的倒数第 N 个结点

快慢指针的思想,不难考虑。

自己代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newHead = new ListNode(-1,head);
        ListNode slow = newHead;
        ListNode fast = newHead;
        while( fast.next != null){
            fast = fast.next;
            n--;
            if(n<0){
                slow = slow.next;
            }
        }
        slow.next = slow.next.next;
        return newHead.next;
    }
}

示例代码:

public ListNode removeNthFromEnd(ListNode head, int n){
    ListNode dummyNode = new ListNode(0);
    dummyNode.next = head;

    ListNode fastIndex = dummyNode;
    ListNode slowIndex = dummyNode;

    // 只要快慢指针相差 n 个结点即可
    for (int i = 0; i <= n  ; i++){ 
        fastIndex = fastIndex.next;
    }

    while (fastIndex != null){
        fastIndex = fastIndex.next;
        slowIndex = slowIndex.next;
    }

    //此时 slowIndex 的位置就是待删除元素的前一个位置。
    //具体情况可自己画一个链表长度为 3 的图来模拟代码来理解
    slowIndex.next = slowIndex.next.next;
    return dummyNode.next;
}
链表相交

链表相交

解法1: 计算两个链表的长度,取差值,让长的链表先走差值步数。随后再挨个判断。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int aSize = 0,bSize = 0;
        ListNode aTemp = headA;
        ListNode bTemp = headB;
        while(aTemp != null){
            aTemp = aTemp.next;
            aSize++;
        }
        while(bTemp != null){
            bTemp = bTemp.next;
            bSize++;
        }
        if(aSize >bSize){
            int n =  aSize -bSize;
            while(n>0){
                headA = headA.next;
                n--;
            }
        }else{
            int n = bSize - aSize;
            while(n>0){
                headB = headB.next;
                n--;
            }
        }
        while(headA != null){
            if(headA == headB) return headA;
            headA = headA.next;
            headB = headB.next;
        }
        return null;

    }
}

解法2:双指针

两个指针pointA,pointB一直向后遍历,如果pointA为空则让pointA指向headB;如果pointB为空则让pointB指向headA;

这样当两个指针都走完的时候,较长的链表指针会指向较短链表的头,而较短的链表指针则会指向较长链表的第n个位置,n为两个链表长度的差值。

剩下的就是边界的判断。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB ==null) return null;
        ListNode pointA = headA;
        ListNode pointB = headB;
        while(pointA !=pointB){
            pointA = pointA == null?headB:pointA.next;
            pointB = pointB == null?headA:pointB.next;
        }
        return pointA;
    }
}
环形链表

环形链表 II

自己代码: 思路用一个哈希表来记录每个链表节点的地址,如果有重复的,则第一个重复的就是环入口节点。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        Map<Integer,Integer> hash =new HashMap<>();
        while(head != null){
            if( hash.get(head.val) == null){
                hash.put(head.val,1);
            }else{
                return head;
            }
            head = head.next;
        }
        return head;
    }
}

官方题解:

快慢指针 + 数学题解

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {// 有环
                ListNode index1 = fast;
                ListNode index2 = head;
                // 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
                while (index1 != index2) {
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值