代码随想录一刷 Day4

24. Swap Nodes in Pairs - 力扣(LeetCode)

1.遍历何时结束?

分为奇数偶数个节点两种情况

设置一个虚拟头结点phead

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head==null||head.next==null)
        return head;
        //
        ListNode phead=new ListNode(-1,head);
        //添加phead是因为2-1-3-4 操作1-3-4为1-4-3时,我们的pre要指向1
        ListNode pre=phead;
        ListNode cur=head;
        while(cur!=null&&cur.next!=null)//剩一个节点的情况和
        {
            pre.next=cur.next;
            ListNode temp=cur.next.next;
            cur.next.next=cur;
            cur.next=temp;
            //调换完毕
            pre=cur;
            cur=cur.next;
        }
        return phead.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;
    }
} 

 19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

这道题和上道题的交换两节点相似,都使用了虚拟头结点,因为我们有一个对节点的操作时,都要操纵当前节点的前一个节点的next指向这个节点

/**
 * 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) {
        if(head==null)
        return head;
        if(head.next==null&&n==1)
        return null;
        //至少有两个节点
        ListNode l=head;
        ListNode f=head;
        ListNode phead=new ListNode(0,head);
        ListNode pre=phead;
        while(n!=0)
        {
            f=f.next;
            n--;
        }
        while(f!=null)
        {
            pre=pre.next;
            l=l.next;
            f=f.next;
        }
        pre.next=l.next;
        return phead.next;
    }
}

面试题 02.07. 链表相交 - 力扣(LeetCode)

"尾端对齐思想"

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //单向链表最大的问题在于只能单向遍历 因此从后向前遍历行不通
        ListNode preA=headA;
        ListNode preB=headB;
        ListNode curA=headA;
        ListNode curB=headB;
        int lenA=0;
        int lenB=0;
        int gap=0;
        while(preA!=null)
        {
            preA=preA.next;
            lenA++;
        }
        while(preB!=null)
        {
            preB=preB.next;
            lenB++;
        }
        if(lenA>lenB)
        {
            //A为长链表
            gap=lenA-lenB;
            while(gap!=0)
            {
                curA=curA.next;
                gap--;
            }
            while(lenB!=0)
            {
                if(curA==curB)
                return curA;
                curA=curA.next;
                curB=curB.next;
                lenB--;
            }
        }else{
            gap=lenB-lenA;
            while(gap!=0)
            {
                curB=curB.next;
                gap--;
            }
            while(lenA!=0)
            {
                if(curA==curB)
                return curA;
                curA=curA.next;
                curB=curB.next;
                lenA--;
            }

        }
            return null;
    }
}

142. 环形链表 II - 力扣(LeetCode)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值