[Leetcode]#206 Reverse Linked List
2015-09-02 07:59
405 查看
//#206 Reverse Linked List //8ms 100% #include <iostream> using namespace std; class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *previous_p, *current_p, *next_p; if(head == NULL) return head; //linked_list size == 0 if(head->next == NULL) return head; //linked_list size == 1 if(head->next->next == NULL) //linked_list size == 2 { head->next->next = head; head = head->next; head->next->next = NULL; return head; } //linked_list size >= 3 previous_p = head; current_p = head->next; next_p = head->next->next; previous_p->next = NULL; while(next_p->next != NULL) //next_p reaching the tail and when jumping out of this while loop //next_p->next == NULL { current_p->next = previous_p; //prepare for next round previous_p = current_p; current_p = next_p; next_p = next_p->next; } current_p->next = previous_p; next_p->next = current_p; head = next_p; return head; } };
相关文章推荐
- 解析Java中的队列和用LinkedList集合模拟队列的方法
- JAVA LinkedList和ArrayList的使用及性能分析
- LinkedList学习示例模拟堆栈与队列数据结构
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- ArrayList和LinkedList的主要区别
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解