LeetCode-探索-初级算法-链表-3. 反转链表(个人做题记录,不是习题讲解)

LeetCode-探索-初级算法-链表-3. 反转链表(个人做题记录,不是习题讲解)

LeetCode探索-初级算法:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/

  1. 反转链表
  • 语言:java

  • 思路:一开始没想好,后面看了下网上的思路,大致就是用多个指针,往后遍历的时候顺便就修改next了。用3个指针分别表示当前,上一个,下一个

  • 代码(0ms):

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode pre = head,current = head,next = head;
            if(next == null)
                return null;
            next = next.next;
            pre.next = null;
            while(next!=null){
                pre = current;
                current = next;
                next = next.next;
                current.next = pre;
            }
            return current;
        }
    }
    
  • 参考代码1(0ms)非递归:要说的话也是3指针,但是它这个判断更统一,更简练。

    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode curr = head;
            ListNode prev = null;
            
            while (curr != null) {
                ListNode temp = curr.next;
                curr.next = prev;
                prev = curr;
                curr = temp;
            }
            
            return prev;
        }
    }
    
  • 参考代码2(8ms)递归:

    https://blog.csdn.net/xiashanrenlaozhang/article/details/80834701

    https://www.cnblogs.com/tengdai/p/9279421.html

    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            // 采用递归的方法,就是使用反转函数反转子链表,直接当做已经反转了。
            if((head == NULL) || (head->next == NULL)){
                return head;
            }
            else{
                ListNode* son_list = reverseList(head->next);
                // 子链表的尾节点指向前一个节点
                head->next->next = head;
                head->next = NULL;
                return son_list;
            }
        }
    };
    
  • 参考后重写1(0ms)非递归:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            ListNode pre = null,current = head;
            while(current!=null){
                ListNode tmp = current.next;
                current.next = pre;
                pre = current;
                current = tmp;
            }
            return pre;
        }
    }
    
  • 参考后重写2(0ms)递归:先判断如果当前节点A是null或其下一节点是null,说明不用反转or已经反转完了,直接返回当前节点A;递归部分:获取A的下一个节点B,由于B要作为下一个递归的A节点,所以用临时变量C=B传入递归函数,B的next=A,A的next=null,返回C;

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode B = head.next;
            ListNode nextHead = reverseList(B); 
            B.next = head;
            head.next = null;
            return nextHead;
        }
    }
    
  • 参考后重写2:递归肯定向后,所以最后到最后一个节点时返回;递归时,每次当前A的下一个节点B的next->A,而A的next->null;因为需要把当前B节点当作下次的A节点,所以使用res递归传入B作为下次递归的A;这样res一定会是最后一个节点,所以返回res

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode reverseList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode res = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return res;
        }
    }
    
发布了60 篇原创文章 · 获赞 6 · 访问量 5542

猜你喜欢

转载自blog.csdn.net/Ashiamd/article/details/102263072