【LeetCode】27.Linked List—Reverse Linked List l链表逆置

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

思路:

重塑链表,将首结点逐个后移,移动过程中遇到的结点都依次结到链表开头。

/**
 * 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) {
        ListNode *p=head;        //定义指向头节点的指针
        if(p==NULL) return NULL; //使用前判断是否为空指针
        ListNode *q=p->next;     //定义第二个指针始终指向P的next
        while(p!=NULL&&q!=NULL)
        {
            p->next=q->next;
            q->next=head;
            head=q;
            q=p->next;
        }
        return head;
    }
};

原文地址:https://www.cnblogs.com/hu-19941213/p/11429659.html

时间: 2024-09-30 10:04:46

【LeetCode】27.Linked List—Reverse Linked List l链表逆置的相关文章

LeetCode[Linked List]: Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode笔记:206. Reverse Linked List

题目: Reverse a singly linked list. 大意: 反转一个简单链表. 思路: 题目的意思就是给出一个链表,本来是从头指向尾的,现在要你做成从尾指向头,并且返回原来的尾,现在的头.这个肯定是要用递归或者迭代来做.只要屡清楚过程,会比较绕.大体的流程就是,把下一个节点的next指向自己,一个个迭代.递归下去,最后返回最后的原来的尾节点 他山之石: 这里给出Discuss中最火的方法. 迭代实现: public ListNode reverseList(ListNode he

LeetCode【92】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 相比翻转整个链表稍微麻烦了那么一点,不过只要考虑好翻转的具体实现,问题都一样.AC代码如下: ListNode* reverse(

141. Linked List Cycle(判断l链表是否有环)(leetcode)

Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in

LeetCode: Reverse Linked List

LeetCode: Reverse Linked List Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follo

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol

LeetCode OJ - Reverse Linked List II

题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ l

[LeetCode][JavaScript]Reverse Linked List II

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following cond

[LeetCode][JavaScript]Reverse Linked List

Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? https://leetcode.com/problems/reverse-linked-list/ 反转链表. 1 /** 2 * Definition