Skip to content

Commit 2863ba0

Browse files
author
zhangruihao.zhang
committed
geet作业提交
1 parent c0531e5 commit 2863ba0

File tree

7 files changed

+383
-0
lines changed

7 files changed

+383
-0
lines changed

Week_01/id_108/LeetCode_142_108.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_142_108 {
7+
/**
8+
* Definition for singly-linked list.
9+
* class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) {
13+
* val = x;
14+
* next = null;
15+
* }
16+
* }
17+
*/
18+
public class Solution {
19+
public ListNode detectCycle(ListNode head) {
20+
if(head == null || head.next == null){
21+
return null;
22+
}
23+
ListNode slow = head;
24+
ListNode quick = head;
25+
while(slow != null && quick != null){
26+
slow = slow.next;
27+
quick = quick == null || quick.next == null ? null : quick.next.next;
28+
if(slow == quick){
29+
break;
30+
}
31+
}
32+
33+
if(slow == null || quick == null){
34+
return null;
35+
}
36+
slow = head;
37+
while(slow != quick){
38+
slow = slow.next;
39+
quick = quick.next;
40+
}
41+
return slow;
42+
}
43+
}
44+
}

Week_01/id_108/LeetCode_21_108.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_21_108 {
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution {
16+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
17+
if(l1 == null){
18+
return l2;
19+
}
20+
if(l2 == null){
21+
return l1;
22+
}
23+
// 链表1的游标
24+
ListNode cur1 = l1;
25+
// 链表2的游标
26+
ListNode cur2 = l2;
27+
//合并后的链表的表头
28+
ListNode head = null;
29+
if(cur1.val < cur2.val){
30+
head = cur1;
31+
cur1 = cur1.next;
32+
}else{
33+
head = cur2;
34+
cur2 = cur2.next;
35+
}
36+
//将head指向null,防止指针影响
37+
head.next = null;
38+
// 合并后的链表的游标
39+
ListNode cur = head;
40+
while (cur1 != null && cur2 != null){
41+
if(cur1.val < cur2.val){
42+
cur.next = cur1;
43+
cur1 = cur1.next;
44+
}else{
45+
cur.next = cur2;
46+
cur2 = cur2.next;
47+
}
48+
//游标后移,并指向空
49+
cur = cur.next;
50+
cur.next = null;
51+
}
52+
if(cur1 != null){
53+
cur.next = cur1;
54+
}
55+
if(cur2 != null){
56+
cur.next = cur2;
57+
}
58+
return head;
59+
}
60+
}
61+
}

Week_01/id_108/LeetCode_24_108.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_24_108 {
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution1 {
16+
public ListNode swapPairs(ListNode head) {
17+
if(head == null || head.next == null){
18+
return head;
19+
}
20+
//标识要交换的第一个节点
21+
ListNode first = head;
22+
//标识要交换的第二个节点
23+
ListNode second = head.next;
24+
//标识下一组要交换的节点对
25+
ListNode cur = second.next;
26+
//交换
27+
first.next = cur;
28+
second.next = first;
29+
head = second;
30+
ListNode pre = first;
31+
//下一组
32+
first = cur;
33+
second = cur == null || cur.next == null ? null : cur.next;
34+
cur = second == null ? null : second.next;
35+
while(first != null && second != null){
36+
//交换
37+
first.next = cur;
38+
second.next = first;
39+
pre.next = second;
40+
pre = first;
41+
//下一组
42+
first = cur;
43+
second = cur == null || cur.next == null ? null : cur.next;
44+
cur = second == null ? null : second.next;
45+
}
46+
return head;
47+
}
48+
}
49+
50+
class Solution2 {
51+
public ListNode swapPairs(ListNode head) {
52+
if(head == null || head.next == null){
53+
return head;
54+
}
55+
//标识要交换的第一个节点
56+
ListNode first = head;
57+
//标识要交换的第二个节点
58+
ListNode second = head.next;
59+
//标识下一组要交换的节点对
60+
ListNode cur = second.next;
61+
//初始化前节点
62+
ListNode pre = null ;
63+
//第一次交换
64+
first.next = cur;
65+
second.next = first;
66+
head = second;
67+
while(true){
68+
pre = first;
69+
first = cur;
70+
second = cur == null || cur.next == null ? null : cur.next;
71+
cur = second == null ? null : second.next;
72+
if(first == null || second == null){
73+
break;
74+
}
75+
swap(pre,first,second,cur);
76+
}
77+
return head;
78+
}
79+
80+
private void swap(ListNode pre,ListNode first,ListNode second,ListNode cur){
81+
//交换
82+
first.next = cur;
83+
second.next = first;
84+
pre.next = second;
85+
}
86+
}
87+
}

Week_01/id_108/LeetCode_25_108.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_25_108 {
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution {
16+
public ListNode reverseKGroup(ListNode head, int k) {
17+
if(k == 1){
18+
return head;
19+
}
20+
if(head == null){
21+
return head;
22+
}
23+
ListNode tail = needReverse(head,k);
24+
if(tail == null){
25+
return head;
26+
}
27+
//下一组要翻转的起点
28+
ListNode nextFirst = tail.next;
29+
tail.next = null;
30+
ListNode[] pair = reverse(head);
31+
head = pair[0];
32+
ListNode lastTail = pair[1];
33+
//翻转起点
34+
ListNode reverseStart = nextFirst;
35+
while(true){
36+
tail = needReverse(nextFirst,k);
37+
if(tail == null){
38+
lastTail.next = nextFirst;
39+
break;
40+
}
41+
//翻转起点
42+
reverseStart = nextFirst;
43+
//跳到未来的下一组
44+
nextFirst = tail.next;
45+
tail.next = null;
46+
pair = reverse(reverseStart);
47+
lastTail.next = pair[0];
48+
lastTail = pair[1];
49+
}
50+
return head;
51+
}
52+
53+
//判断链表是否可以翻转,如果可以翻转,则返回翻转链表的最后一个节点
54+
private ListNode needReverse(ListNode head,int k){
55+
int i = 1;
56+
ListNode cur = head;
57+
while(i<k && cur != null && cur.next != null){
58+
cur = cur.next;
59+
i++;
60+
}
61+
return i == k ? cur : null;
62+
}
63+
64+
//返回翻转后的链表的首尾对
65+
private ListNode[] reverse(ListNode head){
66+
ListNode pre = head;
67+
ListNode cur = head.next;
68+
ListNode tmp = cur == null ? null : cur.next;
69+
head.next = null;
70+
71+
while(cur != null){
72+
cur.next = pre;
73+
pre = cur;
74+
cur = tmp;
75+
tmp = tmp == null ? null : tmp.next;
76+
}
77+
return new ListNode[]{pre,head};
78+
}
79+
}
80+
81+
}

Week_01/id_108/LeetCode_83_108.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_83_108 {
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
class Solution {
16+
public ListNode deleteDuplicates(ListNode head) {
17+
if(head == null || head.next == null){
18+
return head;
19+
}
20+
ListNode pre = head;
21+
ListNode tmp = head.next;
22+
while(pre != null){
23+
if(tmp != null && pre.val == tmp.val){
24+
pre.next = tmp.next;
25+
tmp.next = null;
26+
tmp = pre.next;
27+
} else {
28+
pre = pre.next;
29+
tmp = pre != null ? pre.next : null;
30+
}
31+
}
32+
return head;
33+
}
34+
}
35+
}

Week_01/id_108/LeetCode_905_108.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_905_108 {
7+
class Solution1 {
8+
public int[] sortArrayByParity(int[] A) {
9+
int left = 0;
10+
int right = A.length - 1;
11+
while(left < right){
12+
while( left< A.length && (A[left] & 1) == 0){
13+
left ++;
14+
}
15+
while( right>0 && (A[right] & 1) == 1){
16+
right --;
17+
}
18+
if(left < right) {
19+
swap(A,left,right);
20+
}
21+
}
22+
return A;
23+
}
24+
25+
private void swap(int[] A,int left,int right){
26+
int tmp = A[left];
27+
A[left] = A[right];
28+
A[right] = tmp;
29+
}
30+
}
31+
32+
class Solution2 {
33+
public int[] sortArrayByParity(int[] A) {
34+
int[] even = new int[A.length];
35+
int[] odd = new int[A.length];
36+
int evenIndex = 0;
37+
int oddIndex = 0;
38+
for(int i=0 ;i<A.length;i++){
39+
if((A[i] & 1) == 0){
40+
even[evenIndex ++] = A[i];
41+
}else{
42+
odd[oddIndex ++] = A[i];
43+
}
44+
}
45+
for(int i=0 ;i<oddIndex;i++){
46+
even[evenIndex ++] = odd[i];
47+
}
48+
return even;
49+
}
50+
}
51+
}

Week_01/id_108/LeetCode_922_108.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author zhangruihao.zhang
3+
* @version v1.0.0
4+
* @since 2019/04/20
5+
*/
6+
public class LeetCode_922_108 {
7+
class Solution {
8+
public int[] sortArrayByParityII(int[] A) {
9+
int oddIndex = 1;
10+
int evenIndex = 0;
11+
int[] tmp = new int[A.length];
12+
for(int i= 0;i<A.length;i++){
13+
if((A[i] & 1) == 0){
14+
tmp[evenIndex] = A[i];
15+
evenIndex = evenIndex + 2;
16+
}else{
17+
tmp[oddIndex] = A[i];
18+
oddIndex = oddIndex + 2;
19+
}
20+
}
21+
return tmp;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)