Leetcode题目总结-Linkedlist-题目206和234

  今天做了两道非常有意思的题目,leetcode上的206和234。这两道题目都是和链表有关的,并且都会运用的reverse list这个思想。


  pro(206): Reverse a singly linked list.


 My solution1: 

如果是数组实现回非常简单,但是linkedlist如何实现?


第一个想法是,新建一个数组,遍历linkedlist中的所有元素,并记录在数组中。再遍历一次,此时修改在linked list中的每一个元素的值. 这个方法需要三次遍历.

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        
        //第一次遍历,是用来计算链表中有多少个元素,即n
        ListNode Num = head;
        int n = 1;
        while(Num.next != null){ //判断链表是否到头
            Num = Num.next;
            n++;
        }
        
        //第二次遍历,是用来在数组中纪录链表中的元素
        ListNode Record = head;
        int[] Val = new int[n];
        for(int i = 0; i < n; i++){
            Val[i] = Record.val;
            Record = Record.next;
        }
        
        //第三次遍历,是用来反转的
        ListNode Reverse = head;
        for(int j = n-1; j >= 0; j--){
            Reverse.val = Val[j] ;
            Reverse = Reverse.next;
        }
        
        return head;
    }
}

  这是完全利用数组,那么如何治只利用链表呢?


  My solution(2): 迭代

   如果是双链表,进行reverse是非常简单的,因此我们可以借鉴双链表的方法来反转链表。这里我们需要三个node, pre , cur, next 分别代表当前,之前和之后的node.

  

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        
        ListNode pre = null;
        ListNode next = null;
        
        
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        
        return pre;
        
    }
}

 

  My solution(3): 递归

  利用递归!

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        
        ListNode cur = head.next;
        ListNode res = reverseList(cur); //res是得到head之后的反转
        
        head.next = null;
        cur.next = head;
        return res;      
        
    }
}


  接下来我们看看leetcode的234题。


  Pro:

Given a singly linked list, determine if it is a palindrome.

Follow up:Could you do it in O(n) time and O(1) space?


  My solution(1):

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值