2021-12-29 206. 反转链表 24. 两两交换链表中的节点 19. 删除链表的倒数第 N 个结点 160. 相交链表

206. 反转链表

/**
 * 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 reverseList(ListNode head) {
        //需要一个中间节点去保存当前修改方向的节点的下一个节点
        //注意:修改指向也是修改当前不断遍历链表不断后移的节点
        ListNode temp=null;
        ListNode cur=null;
        while(head!=null){
            temp=head.next;
            head.next=cur;
            cur=head;
            head=temp;
        }
    return cur;
    //return head;
    }
}

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) { 
        if(head==null || head.next==null) return head;
        ListNode cur=new ListNode(0,head);
        ListNode res=cur.next.next;
            
        while(cur.next!=null &&cur.next.next!=null){
            ListNode n1=cur.next;
            ListNode n2=cur.next.next.next;
            cur.next=cur.next.next;
            cur.next.next=n1;
            //n1.next=n2;
            cur.next.next.next=n2;
            cur=cur.next.next;
        }

    return res;
    }
}

19. 删除链表的倒数第 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) {
        int l=0;
        ListNode temp=head;
        while(temp!=null){
            l++;
            temp=temp.next;
        }
        ListNode cur=head;
        if(n==l){
          head=head.next;

        }else{
        for(int i=0;i<l-n-1;i++){
            cur=cur.next;
        }
         cur.next=cur.next.next;
        }
       
        return head;
    }
}

160. 相交链表

/**
 * 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 n1=headA;
        ListNode n2=headB;
        while(n1!=n2){
            n1= n1==null ?headB :n1.next;
            n2= n2==null ?headA :n2.next;
        }
        return n1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值