Posts

Showing posts with the label ListNode

451. Swap Nodes in Pairs

ListNode * swapPairs (ListNode * head) { // write your code here //要用dummy, 要打断三个link,也就重建三个link。dummy->1->2->3,变成dummy -> 2->1->3 if (head == NULL || head->next == NULL ){ return head; } ListNode *dummy = new ListNode( 0 ); dummy->next = head; ListNode *pre = dummy; while (head != NULL && head->next != NULL ){ ListNode *temp = head->next->next; head->next->next = head; //重建第一个link pre->next = head->next; //重建第二个link head->next = temp; //重建第三个link pre = head; //更新,准备下一次的重建 head = temp; } return dummy->next; }

96. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Have you met this question in a real interview?    Yes Problem Correction Example   Example 1: Input: list = null, x = 0 Output: null Explanation: The empty list Satisfy the conditions by itself. Example 2: Input: list = 1->4->3->2->5->2->null, x = 3 Output: 1->2->2->4->3->5->null Explanation: keep the original relative order of the nodes in each of the two partitions. class Solution { public : /** * @param head: The first node of linked list * @param x: An integer * @return: A ListNode */ ListNode * partition (ListNode * head, int x) { // write your code here if (head == NULL ){ return NULL ; } ...

106. Convert Sorted List to Binary Search Tree

class Solution { public : /* * @param head: The first node of linked list. * @return: a tree node */ TreeNode * sortedListToBST (ListNode * head) { // write your code here // Divide and conquer //1, slow and fast pointers finding the mid //2, pick the middle as root node //3, Divide //4, conquer if (head == NULL ){ return NULL ; } if (head->next == NULL ){ TreeNode *root = new TreeNode(head->val); return root; } ListNode *dummy = new ListNode( 0 ); dummy->next = head; ListNode *copy = head; ListNode *slow = head; ListNode *fast = head->next; ListNode *pre = dummy; while (fast != NULL && fast->next != NULL ){ pre = slow; slow = slow->next; fast = fast->next->next; } pre->next = NULL...

lintcode 60. Search Insert Position

class Solution { public : /** * @param A: an integer sorted array * @param target: an integer to be inserted * @return: An integer */ int searchInsert ( vector < int > &A, int target) { // write your code here // search the first element that >= target int sizeA = A.size(); if (sizeA == 0 ){ return 0 ; } if (A[ 0 ] >= target){ return 0 ; } if (A[sizeA - 1 ] < target){ return sizeA; } int start = 0 ; int end = sizeA - 1 ; while (start + 1 < end){ int mid = (start + end) / 2 ; if (A[mid] >= target){ end = mid; } else { start = mid; } } if (A[start] == target){ return start; } if (A[end] == target){ return end; } if (...

lintcode 170. Rotate List

class Solution { public : /** * @param head: the List * @param k: rotate to the right k places * @return: the list after rotation */ ListNode * rotateRight (ListNode * head, int k) { // write your code here if (head == NULL || head->next == NULL ){ return head; } int len = 0 ; ListNode *copy = head; while (head != NULL ){ len++; head = head->next; } int realK = k % len; if (realK == 0 ){ return copy; } ListNode *copy2 = copy; for ( int i = 0 ; i < len - realK - 1 ; i++){ copy = copy->next; } ListNode *newHead = copy->next; copy->next = NULL ; ListNode *newHeadCopy = newHead; while (newHead->next != NULL ){ newHead = newHead->next; } newHead->next = copy2; return newHe...

451. Swap Nodes in Pairs

Code ( Language :C++) Edit Description 中文 English Given a linked list, swap every two adjacent nodes and return its head. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: 1->2->3->4->null Output: 2->1->4->3->null /** * Definition of singly-linked-list: * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */ class Solution { public : /** * @param head: a ListNode * @return: a ListNode */ ListNode * swapPairs (ListNode * head) { // write your code here // 1, divided into two list nodes // 2, merge if (head == NULL || head->next == NULL ){ return head; } ListNode *head1 = head; ListNode *head2 = head->next; ListNode *head1...