LEETCODE 206

来源:互联网 发布:手机txt制作软件 编辑:程序博客网 时间:2024/04/26 13:00


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?

Subscribe to see which companies asked this question

旋转指针,比较简单的题。

/** * 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* pre = NULL;        ListNode* nxt = head;        while(nxt)        {            ListNode* temp = nxt->next;            nxt->next = pre;            pre = nxt;            nxt = temp;        }        return pre;    }};


0 0
原创粉丝点击