leetcode——链表

  1. 206. 反转链表(易)

    反转一个单链表。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL
     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* head) {
    12         ListNode *newHead=NULL;
    13         while(head){
    14             ListNode *next=head->next;
    15             head->next=newHead;
    16             newHead=head;
    17             head=next;
    18         }
    19         return newHead;
    20     }
    21 };
    View Code

猜你喜欢

转载自www.cnblogs.com/mcq1999/p/11717051.html