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种常见语言的答案。

posted @ 2021-08-10 18:26  甜甜嘟嘟  阅读(158)  评论(0编辑  收藏  举报