LeeCode打卡第十四天

LeeCode打卡第十四天

第一题:两两交换链表中的节点(LeeCode第24题):

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。


解法一:

主要思想:首先设置一个头节点指向链表中的第一个元素,接着挨个分析交换列表中的两元素,链表中的指针应该怎么变化,注意临时指针的保存,还有临界值的判断

/**
 * 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) {
        ListNode dummyhead = new ListNode(-1);
        dummyhead.next = head;
        ListNode cur = dummyhead;
        while(cur.next != null && cur.next.next != null){
            ListNode tmp1 = cur.next;
            ListNode tmp2 = cur.next.next.next;
            cur.next = cur.next.next;
            cur.next.next = tmp1;
            cur.next.next.next = tmp2;
            cur = tmp1;
        }
        return dummyhead.next;
    }
}

第二题:相交链表(LeeCode第160题):

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表、 # LeeCode打卡第十四天

第一题:两两交换链表中的节点(LeeCode第24题):

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。


解法一:

主要思想:首先设置一个头节点指向链表中的第一个元素,接着挨个分析交换列表中的两元素,链表中的指针应该怎么变化,注意临时指针的保存,还有临界值的判断

/**
 * 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) {
        ListNode dummyhead = new ListNode(-1);
        dummyhead.next = head;
        ListNode cur = dummyhead;
        while(cur.next != null && cur.next.next != null){
            ListNode tmp1 = cur.next;
            ListNode tmp2 = cur.next.next.next;
            cur.next = cur.next.next;
            cur.next.next = tmp1;
            cur.next.next.next = tmp2;
            cur = tmp1;
        }
        return dummyhead.next;
    }
}

第二题:相交链表(LeeCode第160题):

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。


在这里插入图片描述

主要思想:结合上面这张图来看,链表A和链表B他们在c1交汇,假设a1-a2的距离为a,b1-b3的距离为b,c1-c3的距离为c,链表A,B都遍历完自己的序列再去遍历对方的,最终会在c1处交汇,此时他们所走的路径都为a+b+c

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p = headA, q = headB;
        while(p != q){
            if(p != null) {
                p = p.next;
            }else p = headB;
            if(q != null) {
                q = q.next;
            }else q = headA;
        }
        return p; 
    }
}

主要思想:结合上面这张图来看,链表A和链表B他们在c1交汇,假设a1-a2的距离为a,b1-b3的距离为b,c1-c3的距离为c,链表A,B都遍历完自己的序列再去遍历对方的,最终会在c1处交汇,此时他们所走的路径都为a+b+c

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p = headA, q = headB;
        while(p != q){
            if(p != null) {
                p = p.next;
            }else p = headB;
            if(q != null) {
                q = q.next;
            }else q = headA;
        }
        return p; 
    }
}
  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值