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種常見語言的答案。