Reverse a Singly LinkedList

Reverse a Singly LinkedList
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

这是面试的时候被问到的一题,还是考察LinkedList的理解,最后如何得到reverse之后的head的。

public Class Solution{

    public static ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }

        ListNode next = head.next;
        head.next = null;
        while(next != null){
            ListNode temp = next.next;
            next.next = head;
            head = next;
            next = temp;
        }

        return head;
    }
}
时间: 2024-11-08 23:21:57

Reverse a Singly LinkedList的相关文章

LeetCode 206 Reverse a singly linked list.

Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 递归的办法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { v

Reverse a singly linked list

Reverse a singly linked list. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode*

Reversed Linked List(Reverse a singly linked list)

struct ListNode { int m_nKey; ListNode* next; } ListNode* reverseList(ListNode* pHead) { ListNode* pReversedHead = nullptr; ListNode* pNode = pHead; ListNode* pPrev = nullptr; while(pNode != nullptr){ ListNode* pNext = pNode->next; if(pNext == nullpt

LeetCode:Reverse LinkedList

problem: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? solution:头插法逆转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN

[算法专题] LinkedList

前段时间在看一本01年出的旧书<effective Tcp/Ip programming>,这个算法专题中断了几天,现在继续写下去. Introduction 对于单向链表(singly linked list),每个节点有?个next指针指向后一个节点,还有一个成员变量用以储存数值:对于双向链表(Doubly LinkedList),还有一个prev指针指向前一个节点.与数组类似,搜索链表需要O(n)的时间复杂度,但是链表不能通过常数时间读取第k个数据.链表的优势在于能够以较?的效率在任意位

Reverse Linked List

题目: Reverse a singly linked list. cpp: class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *p3 = head->next->next; p1->next = nullpt

206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/#/description Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Hint: The key point is to make a copy of the next link of c

Leetcode-206 Reverse Linked List

#206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNo

leetcode:Reverse Linked List

Reverse a singly linked list. 代码如下: the iterative solution:(c++) /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseLis