File tree Expand file tree Collapse file tree 4 files changed +143
-0
lines changed
Expand file tree Collapse file tree 4 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 1818+ [ 21 Merge Two Sorted Lists(归并)] ( algorithms/MergeTwoSortedLists )
1919+ [ 22 Generate Parentheses(递归、搜索)] ( algorithms/GenerateParentheses )
2020+ [ 23 Merge k Sorted Lists(堆)] ( algorithms/MergekSortedLists )
21+ + [ 24 Swap Nodes in Pairs(链表操作)] ( algorithms/SwapNodesinPairs )
2122+ [ 26 Remove Duplicates from Sorted Array] ( algorithms/RemoveDuplicatesfromSortedArray )
2223+ [ 27 Remove Element] ( algorithms/RemoveElement )
2324+ [ 29 Divide Two Integers(位运算、最小负数绝对值、溢出检测)] ( algorithms/DivideTwoIntegers )
Original file line number Diff line number Diff line change 1+ ## Swap Nodes in Pairs
2+
3+ Given a linked list, swap every two adjacent nodes and return its head.
4+
5+ For example,
6+ ```
7+ Given 1->2->3->4, you should return the list as 2->1->4->3.
8+ ```
9+
10+ Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
11+
12+ ## Solution
13+
14+ 用三个指针p, q, pre, p指向当前节点,初始化为head,q指向p的下一个节点,pre指向上次完成的节点,初始化null
15+
16+ 交换p,q,同时需要修改pre的下一个节点(之前指向p,由于p和q交换位置,需要修改指向q):
17+
18+ ``` cpp
19+ while (p && p->next) {
20+ ListNode *q = p->next;
21+ p->next = q->next;
22+ q->next = p;
23+ if (pre)
24+ pre->next = q;
25+ pre = p;
26+ p = p->next;
27+ }
28+ ```
29+
30+ 同时需要注意更新head指针。完整代码:
31+
32+ ``` cpp
33+ ListNode *swapPairs (ListNode * head) {
34+ if (head == nullptr || head->next == nullptr)
35+ return head;
36+ ListNode * p = head;
37+ head = p->next;
38+ ListNode * pre = nullptr;
39+ while (p && p->next) {
40+ ListNode * q = p->next;
41+ p->next = q->next;
42+ q->next = p;
43+ if (pre)
44+ pre->next = q;
45+ pre = p;
46+ p = p->next;
47+ }
48+ return head;
49+ }
50+ ```
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < cstdio>
3+ #include < vector>
4+ #include < algorithm>
5+ #include < string>
6+ using namespace std ;
7+ #include < stdlib.h>
8+ struct ListNode {
9+ int val;
10+ ListNode *next;
11+ ListNode (int x): val(x), next(nullptr ) {}
12+ };
13+ class Solution {
14+ public:
15+ ListNode *swapPairs (ListNode *head) {
16+ if (head == nullptr || head->next == nullptr )
17+ return head;
18+ ListNode *p = head;
19+ head = p->next ;
20+ ListNode *pre = nullptr ;
21+ while (p && p->next ) {
22+ ListNode *q = p->next ;
23+ p->next = q->next ;
24+ q->next = p;
25+ if (pre )
26+ pre ->next = q;
27+ pre = p;
28+ p = p->next ;
29+ }
30+ return head;
31+ }
32+
33+ };
34+ int getLength (ListNode *head)
35+ {
36+ int len = 0 ;
37+ ListNode *p = head;
38+ while (p) {
39+ ++len;
40+ p = p->next ;
41+ }
42+ return len;
43+ }
44+ void print (ListNode *head)
45+ {
46+ if (head == nullptr ) {
47+ printf (" NULL\n " );
48+ return ;
49+ }
50+ struct ListNode *p = head;
51+ while (p) {
52+ printf (" %d " , p->val );
53+ p = p->next ;
54+ }
55+ printf (" \n " );
56+ }
57+ ListNode * mk_list (ListNode **ha, int a[], int n)
58+ {
59+ if (n < 1 )
60+ return nullptr ;
61+ ListNode *p = new ListNode (a[0 ]);
62+ *ha = p;
63+ for (int i = 1 ; i < n; ++i) {
64+ ListNode *q = new ListNode (a[i]);
65+ p->next = q;
66+ p = q;
67+ }
68+ return p;
69+ }
70+ void free_list (struct ListNode *head)
71+ {
72+ struct ListNode *p = head;
73+ while (p) {
74+ struct ListNode *q = p->next ;
75+ delete p;
76+ p = q;
77+ }
78+ }
79+ int main (int argc, char **argv)
80+ {
81+ Solution solution;
82+ struct ListNode *head = NULL ;
83+ int a[] = {1 , 2 };
84+ mk_list (&head, a, 2 );
85+ head = solution.swapPairs (head);
86+ print (head);
87+ return 0 ;
88+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ struct ListNode {
1010 ListNode *next;
1111 ListNode (int x): val(x), next(nullptr ) {}
1212};
13+ class Solution {
14+ public:
15+
16+ };
1317int getLength (ListNode *head)
1418{
1519 int len = 0 ;
You can’t perform that action at this time.
0 commit comments