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 == nullptr)
            pReversedHead = pNode;
        pNode->next = pPrev;

        pPrev = pNode;
        pNode = pNext;
    }

    return pReversedHead;
}

//备注:递归实现方式待完善?

原文地址:https://www.cnblogs.com/hujianglang/p/11421013.html

时间: 2024-10-08 20:11:46

Reversed Linked List(Reverse a singly linked list)的相关文章

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*

【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? 思路: 重塑链表,将首结点逐个后移,移动过程中遇到的结点都

Linked List {singly linked list -> doubly linked list -> circular linked list}

Linked List 单向链表 Singly linked list /********************************************************* Code writer : EOF Code file : single_linked_list.c Code date : 2015.01.15 e-mail : [email protected] Code description: Here is a implementation for singly

单链表反转(Singly Linked Lists in Java)

单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 Java代码   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } Java代码   package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[])

[cc150] check palindrome of a singly linked list

Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse and compare. 另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形式: 0 ( 1 ( 2 ( 3 ) 2 ) 1 ) 0 0 ( 1 ( 2 ( 3 3 ) 2 ) 1 ) 0 CC150里面给出的Code,非常简洁,贴在下面: length == 1 对应

Singly Linked List

Singly linked list storage structure:typedef struct Node{ ElemType data; struct Node *next;}Node; typedef struct Node *LinkList; LinkedList without head node: LinkedList with head node: Operations: /*check the size of link list.*/int ListLength(LinkL

To find the kth to Last Element of a Singly Linked List

To find the kth to Last Element of a Singly Linked List To find the kth to Last Element of a Singly Linked List Web Link Description Code - C Tips Web Link None Description Write a pro-gram to find the kth to Last Ele-ment of a Singly Linked List For

LeetCode[Linked List]: Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod