zoukankan      html  css  js  c++  java
  • 2021字节跳动校招秋招算法面试真题解题报告--leetcode206 反转链表,内含7种语言答案

    206.反转链表

    1.题目描述

    反转一个单链表。


    示例:


    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL

    进阶:

    你可以迭代或递归地反转链表。你能否用两种方法解决这道题?


    2.解题报告

    思路1:借助栈

    利用栈先进后出的特点,将每个节点按顺序存入栈中,再从顶到底连接栈中的每个节点
    注意要将翻转后的最后一个节点(即原链表的第一个节点)的next置为nullptr,不然后果可想而知~

    思路2:就地操作(推荐)

    逐个断开原链表的每个节点(保存下个节点)
    将断开的节点连接到反转链表的表头上
    更新反转链表的表头
    回到原链表的下个节点

    3.最优答案

    c答案

    
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    //struct ListNode* reverseList(struct ListNode* head) {
    //    struct ListNode* new = NULL;
    //    while(head) {
    //        struct ListNode* temp = head;
    //        head = head->next;
    //        temp->next = new;
    //        new = temp;
    //    }
    //    return new;
    //}
    
    struct ListNode* reverseList(struct ListNode* head) {
        if (!head || !head->next) return head;
        struct ListNode *L = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return L;
    }
    
    

    c++答案

    
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if (!head || !head->next) return head;
            
            ListNode *node = reverseList(head->next);
            head->next->next = head;
            head->next = NULL;
            
            return node;
        }
    };
    
    

    java答案

    
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    
    class DIYStack{
        public ArrayList<Integer> container = new ArrayList<>();
        public void comeIn(int item){
            container.add(item);
        }
        public int comeOut(){
            return container.remove(container.size()-1);
        }
    }
    class Solution {
        public ListNode reverseList(ListNode head) {
            DIYStack diyStack = new DIYStack();
            ListNode tmp = head;
            while (tmp != null){
                diyStack.comeIn(tmp.val);
                tmp = tmp.next;
            }
            
            tmp = head;
            while (tmp != null) {
                tmp.val = diyStack.comeOut();
                tmp = tmp.next;
            }
            return head;
        }
    }
    
    

    JavaScript答案

    
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @return {ListNode}
     */
    var reverseList = function(head) {
    
        var cur = head;
        var prev = null;
        while(cur){
            var next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    };
    
    

    c#答案

    
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode ReverseList(ListNode head) {
            
            ListNode b = null;
            ListNode Nextindex;
            while(head != null)
            {
                Nextindex = head.next;
                head.next = b;
                b=head;
                head = Nextindex;
            }
            return b;
        }
    }
    
    

    python2.x答案

    
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head is None or head.next is None:
                return head
            stack = []
            while head:
                stack.append(head)
                head = head.next
            newHead = stack[-1]
            # while stack:
            #     now = stack.pop()
            for i in range(len(stack) - 1, 0, -1):
                stack[i].next = stack[i - 1]
            stack[0].next = None
            return newHead
    
    

    python3.x答案

    
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head is None:
                return None
            if head.next is None:
                return head
            p = head
            q = None
            while p:
                tmp = p.next
                p.next = q
                q = p
                p = tmp
            return q
                
            
    
    

    go答案

    
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func reverseList(head *ListNode) *ListNode {
        var preNode *ListNode = nil
        var currentNode *ListNode = head
        
        for currentNode != nil {
            nextNode := currentNode.Next
            
            currentNode.Next = preNode
            
            preNode = currentNode
            
            currentNode = nextNode
        }
        return preNode
        
    }
    
    

    4.leetcode解题报告合集

    leetcode最佳答案
    leetcode是练习算法能力修炼编程内功非常好的平台,但是官网上解题报告不全,网上的答案也有对有错,为了提升大家刷题的效率,现将每个题目的各种语言的答案(通过测试)总结发布出来,供大家参考。每个题目包含c、c++、java、 python、 python3、 javascript、 c#、 go 8种常见语言的答案。

  • 相关阅读:
    。【自学总结 2】------3ds Max 菜单
    。【自学总结 1】------3ds Max 界面
    。求推荐一个usb集线器的购买网址
    (翻译)Importing models-Models
    (翻译)Importing models-FBX Importer, Rig options
    [游戏开发-学习笔记]菜鸟慢慢飞(18)-迷宫
    [游戏开发-学习笔记]菜鸟慢慢飞(17)- c#-委托简记(匿名方法,Lambda表达式)
    [游戏开发-学习笔记]菜鸟慢慢飞(16)- Unity3D-Android插件问题集锦
    [游戏开发-学习笔记]菜鸟慢慢飞(15)- Unity3D-图片压缩
    [游戏开发-学习笔记]菜鸟慢慢飞(14)- ScrollView刷新
  • 原文地址:https://www.cnblogs.com/herso/p/15125330.html
Copyright © 2011-2022 走看看