1. list reversal,
// 3-pointer, in situ reverse
/** * 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, *cur = head, *nxt = NULL; while(cur) { nxt = cur->next; cur->next = pre; pre = cur; cur = nxt; } return pre; } };
2. Turn the chain to the node n m, require a complete traversal (leetcode92)
* ReverseBetween the Node (the Node head *, int m, int n-) { the Node H ( 0 ); h.next = head; // provided a head node, the processing of m = 1, the Node * = P & H; the Node * tail ; for ( int I = . 1 ; I <= n-; I ++ ) IF (I <m) // P n-1 points to the first node positions P = p-> Next; the else IF (I == m) // tail point of the n-th node that a reversal in the final portion of the inverted tail = p-> Next; the else {// each time a node tail out later, on the back of p, i.e., the first interpolation method the Node * = tail- Item> Next; tail -> Next = tail-> next-> Next; Item -> Next = p- > Next; P -> = Next Item; } return h.next; }
3. adding two lists (leetcode2)
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode vhead(0), *p = &vhead; int carry = 0; while(l1 || l2 || carry) { int tmp = carry; if(l1 != NULL) tmp += l1->val; if(l2 != NULL) tmp += l2->val; carry = tmp / 10; tmp %= 10; ListNode* next = l1 ? l1 : l2; // 交错 if(next == NULL) next = new ListNode(tmp); // 进位 next->val = tmp; p->next = next; p = p->next; l1 = l1 ? l1->next : NULL; l2 = l2 ? l2->next : NULL; } return vhead.next; }
4. The request list in the penultimate node K
An intuitive idea is to traverse the list to obtain the length of the list, then the second pass can find the k-th node, but requires two traverse, is not a good method;
as used herein, two pointers implemented next iteration of the first pointers go step k-1, the second pointer has been fixed; moved simultaneously then two pointers, a first pointer to know traversal completion. Because two pointers interval k-1, so the second pointer points to node k is the penultimate node.
----------------
Disclaimer: This article is the original article CSDN blogger "Mr. trees blog", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/Koala_Tree/article/details/79011152
ListNode* getKthFromEnd(ListNode* head, int k) { ListNode* p1 = head, *p2 = head; for(int i = 0;i < k-1;i++) p2 = p2->next; while(p2->next) { p1 = p1->next; p2 = p2->next; } return p1; }
5. Analyzing two lists intersect leetcode160
A thought: The easiest to traverse again get lenA, lenB, let's first long gone bad, go resynchronization
Two ideas: to take both, first null to another, and then both gone bad, but also to the length of a single null the change to another, then the rest of the same length.
Three ideas: one of the end to end, transformed into the inlet ring to find
// 思路二 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode* pa = headA, *pb = headB; while(pa != pb) { pa = pa == NULL ? headB : pa->next; pb = pb == NULL ? headA : pb->next; } return pa; }
5.