92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL
提示:pre为第m-1个结点,通过结点指针move = last -> next,使得move指向的结点移动到pre后,cur指向的结点即为反转后的第n个结点。
答案:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode dummy = ListNode(0); dummy.next = head; ListNode *pre = &dummy; for(int i = 0; i < m - 1; i++) pre = pre -> next; ListNode *cur = pre -> next; for(int i = 0; i < n - m; i++){ ListNode *move = cur -> next; cur -> next = move -> next; move -> next = pre -> next; pre -> next = move; } return dummy.next; } };
109. Convert Sorted List to Binary Search Tree
提示:
答案:
138. Copy List with Random Pointer
提示:
答案:
141. Linked List Cycle
提示:
答案:
142. Linked List Cycle II
提示:
答案:
143. Reorder List
提示:
答案:
147. Insertion Sort List
提示:
答案:
148. Sort List
提示:
答案:
160. Intersection of Two Linked Lists
提示:
答案: