day9【代码随想录】反转链表


一、反转链表(力扣206)

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
在这里插入图片描述
在这里插入图片描述

1、头插法

/**
 * 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 dummy = new ListNode(-1,head);
        ListNode temp;
        if(head==null){
            return head;
        }
        ListNode current=head.next;
        head.next=null;
        while(current!=null){
            temp=current.next;
            current.next=dummy.next;
            dummy.next=current;
            current=temp;
        }
    return dummy.next;
    }
}

在这里插入图片描述

2、双指针法

在这里插入图片描述
注意:最后返回新链表的头结点!!!

/**
 * 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 cur = head;
        ListNode pre = null;
        ListNode temp;

        //遍历的过程 什么时候遍历结束 cur==null时 遍历结束
        while(cur!=null){
            temp = cur.next;
            cur.next=pre;
            pre = cur;
            cur = temp;
        }
        return pre; //返回新链表的头结点 此时pre是当前新链表的头结点 不再是head!
    }
}

在这里插入图片描述

3、递归法

/**
 * 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) {
       return reverse(null,head);
    }

    public ListNode reverse(ListNode pre,ListNode cur){
        if(cur==null){
            return pre;  //新链表的头结点
        }
        ListNode temp;
        temp = cur.next;
        cur.next=pre;
       return reverse(cur,temp);
    }
}

在这里插入图片描述

二、回文链表(力扣234)

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
在这里插入图片描述
如果再定义一个新的链表,实现链表元素的反转,然后进行比较:
但是运行有错:

/**
 * 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 boolean isPalindrome(ListNode head) {
        ListNode oldHead = head;
        ListNode newHead = reverse(oldHead);   
        while(newHead!=null&&head!=null){
            if(newHead.val!=head.val){
                return false;
            }
            head=head.next;
            newHead=newHead.next;
        }
        return true;    
    }
    public ListNode reverse(ListNode head){
        ListNode cur=head.next;
        ListNode pre = null;
        ListNode temp;
        while(cur!=null){
            temp=cur.next;
            cur.next=pre;
            pre=cur;
            cur=temp;
        }
        return pre;
    }
}

在这里插入图片描述
未解决!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值