File tree 12 files changed +126
-123
lines changed
12 files changed +126
-123
lines changed Original file line number Diff line number Diff line change 19
19
20
20
## 解法
21
21
22
+ 双指针。
23
+
22
24
<!-- tabs:start -->
23
25
24
26
### ** Python3**
25
27
26
28
``` python
27
29
class Solution :
28
30
def exchange (self , nums : List[int ]) -> List[int ]:
29
- res = [0 for _ in range (len (nums))]
30
31
p, q = 0 , len (nums) - 1
31
- for e in nums:
32
- if (e & 1 ) == 0 :
33
- res[q] = e
34
- q -= 1
35
- else :
36
- res[p] = e
32
+ while p < q:
33
+ if nums[p] & 1 == 1 :
37
34
p += 1
38
- return res
35
+ continue
36
+ if nums[q] & 1 == 0 :
37
+ q -= 1
38
+ continue
39
+ nums[p], nums[q] = nums[q], nums[p]
40
+ return nums
39
41
```
40
42
41
43
### ** Java**
42
44
43
45
``` java
44
46
class Solution {
45
47
public int [] exchange (int [] nums ) {
46
- int len = nums. length;
47
- int [] res = new int [len];
48
- int p = 0 , q = len - 1 ;
49
- for (int e : nums) {
50
- if ((e & 1 ) == 0 ) {
51
- res[q-- ] = e;
52
- } else {
53
- res[p++ ] = e;
48
+ int p = 0 , q = nums. length - 1 ;
49
+ while (p < q) {
50
+ if ((nums[p] & 1 ) == 1 ) {
51
+ ++ p;
52
+ continue ;
54
53
}
54
+ if ((nums[q] & 1 ) == 0 ) {
55
+ -- q;
56
+ continue ;
57
+ }
58
+ swap(nums, p, q);
55
59
}
56
- return res;
60
+ return nums;
61
+ }
62
+
63
+ private void swap (int [] nums , int p , int q ) {
64
+ int t = nums[p];
65
+ nums[p] = nums[q];
66
+ nums[q] = t;
57
67
}
58
68
}
59
69
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int [] exchange (int [] nums ) {
3
- int len = nums .length ;
4
- int [] res = new int [len ];
5
- int p = 0 , q = len - 1 ;
6
- for (int e : nums ) {
7
- if ((e & 1 ) == 0 ) {
8
- res [q --] = e ;
9
- } else {
10
- res [p ++] = e ;
3
+ int p = 0 , q = nums .length - 1 ;
4
+ while (p < q ) {
5
+ if ((nums [p ] & 1 ) == 1 ) {
6
+ ++p ;
7
+ continue ;
11
8
}
9
+ if ((nums [q ] & 1 ) == 0 ) {
10
+ --q ;
11
+ continue ;
12
+ }
13
+ swap (nums , p , q );
12
14
}
13
- return res ;
15
+ return nums ;
16
+ }
17
+
18
+ private void swap (int [] nums , int p , int q ) {
19
+ int t = nums [p ];
20
+ nums [p ] = nums [q ];
21
+ nums [q ] = t ;
14
22
}
15
23
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def exchange (self , nums : List [int ]) -> List [int ]:
3
- res = [0 for _ in range (len (nums ))]
4
3
p , q = 0 , len (nums ) - 1
5
- for e in nums :
6
- if (e & 1 ) == 0 :
7
- res [q ] = e
8
- q -= 1
9
- else :
10
- res [p ] = e
4
+ while p < q :
5
+ if nums [p ] & 1 == 1 :
11
6
p += 1
12
- return res
7
+ continue
8
+ if nums [q ] & 1 == 0 :
9
+ q -= 1
10
+ continue
11
+ nums [p ], nums [q ] = nums [q ], nums [p ]
12
+ return nums
Original file line number Diff line number Diff line change 12
12
13
13
## 解法
14
14
15
- 定义 ` p ` 、` q ` 指针指向 ` head ` 。
15
+ 定义快慢指针 ` slow ` 、` fast ` ,初始指向 ` head ` 。
16
16
17
- ` p ` 先向前走 ` k ` 步,接着 ` p ` 、` q ` 同时向前走,当 ` p ` 指向 ` null ` 时,` q ` 指向的节点即为链表的倒数第 ` k ` 个节点。
17
+ ` fast ` 先向前走 ` k ` 步,接着 ` slow ` 、` fast ` 同时向前走,当 ` fast ` 指向 ` null ` 时,` slow ` 指向的节点即为链表的倒数第 ` k ` 个节点。
18
18
19
19
<!-- tabs:start -->
20
20
29
29
30
30
class Solution :
31
31
def getKthFromEnd (self , head : ListNode, k : int ) -> ListNode:
32
- if not (head or head.next):
33
- return head
34
-
35
- p = q = head
32
+ slow = fast = head
36
33
for _ in range (k):
37
- p = p.next
38
- while p:
39
- p = p.next
40
- q = q.next
41
- return q
42
-
34
+ fast = fast.next
35
+ while fast:
36
+ slow = slow.next
37
+ fast = fast.next
38
+ return slow
43
39
```
44
40
45
41
### ** Java**
@@ -55,18 +51,15 @@ class Solution:
55
51
*/
56
52
class Solution {
57
53
public ListNode getKthFromEnd (ListNode head , int k ) {
58
- if (head == null || head. next == null ) {
59
- return head;
60
- }
61
- ListNode p = head, q = head;
54
+ ListNode slow = head, fast = head;
62
55
while (k-- > 0 ) {
63
- p = p . next;
56
+ fast = fast . next;
64
57
}
65
- while (p != null ) {
66
- p = p . next;
67
- q = q . next;
58
+ while (fast != null ) {
59
+ slow = slow . next;
60
+ fast = fast . next;
68
61
}
69
- return q ;
62
+ return slow ;
70
63
}
71
64
}
72
65
```
Original file line number Diff line number Diff line change 8
8
*/
9
9
class Solution {
10
10
public ListNode getKthFromEnd (ListNode head , int k ) {
11
- if (head == null || head .next == null ) {
12
- return head ;
13
- }
14
- ListNode p = head , q = head ;
11
+ ListNode slow = head , fast = head ;
15
12
while (k -- > 0 ) {
16
- p = p .next ;
13
+ fast = fast .next ;
17
14
}
18
- while (p != null ) {
19
- p = p .next ;
20
- q = q .next ;
15
+ while (fast != null ) {
16
+ slow = slow .next ;
17
+ fast = fast .next ;
21
18
}
22
- return q ;
19
+ return slow ;
23
20
}
24
21
}
Original file line number Diff line number Diff line change 6
6
7
7
class Solution :
8
8
def getKthFromEnd (self , head : ListNode , k : int ) -> ListNode :
9
- if not (head or head .next ):
10
- return head
11
-
12
- p = q = head
9
+ slow = fast = head
13
10
for _ in range (k ):
14
- p = p .next
15
- while p :
16
- p = p .next
17
- q = q .next
18
- return q
11
+ fast = fast .next
12
+ while fast :
13
+ slow = slow .next
14
+ fast = fast .next
15
+ return slow
Original file line number Diff line number Diff line change 17
17
18
18
## 解法
19
19
20
- 定义指针 ` p ` 、 ` q ` 分别指向头节点和下一个节点, ` pre ` 指向头节点的前一个节点 。
20
+ 定义指针 ` pre ` , ` cur ` 分别指向 null 和头节点 。
21
21
22
- 遍历链表,改变指针 ` p ` 指向的节点的指向,将其指向 ` pre ` 指针指向的节点,即 ` p .next = pre` 。然后 ` pre ` 指针指向 ` p ` ,` p ` 、 ` q ` 指针往前走。
22
+ 遍历链表,将 ` cur.next ` 临时保存到 ` t ` 中,然后改变指针 ` cur ` 指向的节点的指向,将其指向 ` pre ` 指针指向的节点,即 ` cur .next = pre` 。然后 ` pre ` 指针指向 ` cur ` ,` cur ` 指针往前走。
23
23
24
24
当遍历结束后,返回 ` pre ` 指针即可。
25
25
36
36
37
37
class Solution :
38
38
def reverseList (self , head : ListNode) -> ListNode:
39
- pre, p = None , head
40
- while p :
41
- q = p .next
42
- p .next = pre
43
- pre = p
44
- p = q
39
+ pre, cur = None , head
40
+ while cur :
41
+ t = cur .next
42
+ cur .next = pre
43
+ pre = cur
44
+ cur = t
45
45
return pre
46
46
```
47
47
@@ -58,13 +58,12 @@ class Solution:
58
58
*/
59
59
class Solution {
60
60
public ListNode reverseList (ListNode head ) {
61
- ListNode pre = null ;
62
- ListNode p = head;
63
- while (p != null ) {
64
- ListNode q = p. next;
65
- p. next = pre;
66
- pre = p;
67
- p = q;
61
+ ListNode pre = null , cur = head;
62
+ while (cur != null ) {
63
+ ListNode t = cur. next;
64
+ cur. next = pre;
65
+ pre = cur;
66
+ cur = t;
68
67
}
69
68
return pre;
70
69
}
Original file line number Diff line number Diff line change 8
8
*/
9
9
class Solution {
10
10
public ListNode reverseList (ListNode head ) {
11
- ListNode pre = null ;
12
- ListNode p = head ;
13
- while (p != null ) {
14
- ListNode q = p .next ;
15
- p .next = pre ;
16
- pre = p ;
17
- p = q ;
11
+ ListNode pre = null , cur = head ;
12
+ while (cur != null ) {
13
+ ListNode t = cur .next ;
14
+ cur .next = pre ;
15
+ pre = cur ;
16
+ cur = t ;
18
17
}
19
18
return pre ;
20
19
}
Original file line number Diff line number Diff line change 6
6
7
7
class Solution :
8
8
def reverseList (self , head : ListNode ) -> ListNode :
9
- pre , p = None , head
10
- while p :
11
- q = p .next
12
- p .next = pre
13
- pre = p
14
- p = q
9
+ pre , cur = None , head
10
+ while cur :
11
+ t = cur .next
12
+ cur .next = pre
13
+ pre = cur
14
+ cur = t
15
15
return pre
Original file line number Diff line number Diff line change 32
32
33
33
class Solution :
34
34
def mergeTwoLists (self , l1 : ListNode, l2 : ListNode) -> ListNode:
35
- dummy = ListNode()
36
- cur = dummy
35
+ dummy = ListNode(0 )
36
+ p = dummy
37
37
while l1 and l2:
38
38
if l1.val <= l2.val:
39
- cur .next = l1
39
+ p .next = l1
40
40
l1 = l1.next
41
41
else :
42
- cur .next = l2
42
+ p .next = l2
43
43
l2 = l2.next
44
- cur = cur .next
45
- cur .next = l1 or l2
44
+ p = p .next
45
+ p .next = l1 or l2
46
46
return dummy.next
47
47
```
48
48
@@ -60,18 +60,18 @@ class Solution:
60
60
class Solution {
61
61
public ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
62
62
ListNode dummy = new ListNode (0 );
63
- ListNode cur = dummy;
63
+ ListNode p = dummy;
64
64
while (l1 != null && l2 != null ) {
65
65
if (l1. val <= l2. val) {
66
- cur . next = l1;
66
+ p . next = l1;
67
67
l1 = l1. next;
68
68
} else {
69
- cur . next = l2;
69
+ p . next = l2;
70
70
l2 = l2. next;
71
71
}
72
- cur = cur . next;
72
+ p = p . next;
73
73
}
74
- cur . next = l1 == null ? l2 : l1;
74
+ p . next = l1 == null ? l2 : l1;
75
75
return dummy. next;
76
76
}
77
77
}
Original file line number Diff line number Diff line change 9
9
class Solution {
10
10
public ListNode mergeTwoLists (ListNode l1 , ListNode l2 ) {
11
11
ListNode dummy = new ListNode (0 );
12
- ListNode cur = dummy ;
12
+ ListNode p = dummy ;
13
13
while (l1 != null && l2 != null ) {
14
14
if (l1 .val <= l2 .val ) {
15
- cur .next = l1 ;
15
+ p .next = l1 ;
16
16
l1 = l1 .next ;
17
17
} else {
18
- cur .next = l2 ;
18
+ p .next = l2 ;
19
19
l2 = l2 .next ;
20
20
}
21
- cur = cur .next ;
21
+ p = p .next ;
22
22
}
23
- cur .next = l1 == null ? l2 : l1 ;
23
+ p .next = l1 == null ? l2 : l1 ;
24
24
return dummy .next ;
25
25
}
26
26
}
You can’t perform that action at this time.
0 commit comments