(leetcode)Reverse Linked List 脑子已经僵住

Reverse a singly linked list.

参考http://www.2cto.com/kf/201110/106607.html

方法1:

讲每个节点的指针指向前面就可以。

/**
 * 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 *q = NULL;
        // if(!(head && head->next)) return NULL;
        // ListNode *p = head->next;
        // head->next = NULL;

        // while(p)
        // {
        //     q=p->next;
        //     p->next=head->next;
        //     head->next=p;
        //     p = q;
        // }
        // return head;

        if((head == NULL) || (head->next==NULL)) return head;
        ListNode *p = head;
        ListNode *q = p->next;
        ListNode *r = NULL;
        head->next = NULL;
        while(q)
        {
            r = q->next;
            q->next = p;
            p = q;
            q = r;
        }
        head = p;
        return head;

    }
};

  方法二:这个方法想了很久才发现题目中的意思head是第一个节点

/**
 * 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==NULL) || (head->next==NULL)) return head;
        ListNode *q = NULL;
        ListNode *p = head->next;//p位置不变

        while(p->next)
        {
            q=p->next;
            p->next = q->next;
            q->next = head->next;
            head->next = q;
        }
        p->next=head;            //相当于成环
        head=p->next->next;       //新head变为原head的next
        p->next->next=NULL;     //断掉环
        return head;

    }
};

  

太菜了!!!!!!

时间: 2024-10-26 18:29:35

(leetcode)Reverse Linked List 脑子已经僵住的相关文章

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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: 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 反转链表区间

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

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: Reverse Linked List II [092]

[题目] 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

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 following condition: 1 ≤ m ≤ n ≤ le

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode 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 sati

[LeetCode] 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? 之前做到Reverse Linked List II 倒置链表之二的时候我还纳闷怎么只有二没有一呢,原来真是忘了啊,现在才加上,这道题跟之前那道比起来简单不少,题目为了增加些许难度,让我们分别用

[LeetCode] Reverse Linked List II @ Python

原题地址:https://oj.leetcode.com/problems/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.