Question:
Reverse a singly linked list.
翻转链表
Algorithm:
见程序
Accepted Code:
/** * 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* l1=NULL; ListNode* l2=head; while(l2) { ListNode* tmp=l2->next; l2->next=l1; l1=l2; l2=tmp; } return l1; } };
时间: 2024-10-29 16:03:42