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

这篇博客详细介绍了如何反转单链表,包括借助栈的辅助和原地操作两种解题思路。提供了C、C++、Java、JavaScript、C#、Python2.x和Python3.x等7种语言的代码实现,并强调了在反转过程中保持链表结构完整性的关键步骤。此外,还提到了LeetCode平台上此类问题的解题报告集合,方便读者拓展学习。
摘要由CSDN通过智能技术生成

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值