Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81d25c9

Browse files
committedNov 13, 2019
init
1 parent 48e1107 commit 81d25c9

10 files changed

+242
-0
lines changed
 

‎icode-leetcode.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

‎pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.fs.leetcode</groupId>
8+
<artifactId>icode-leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
ListNode(int x) {
8+
val = x;
9+
}
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Given a linked list, determine if it has a cycle in it.
5+
* <p>
6+
* Input: head = [3,2,0,-4], pos = 1
7+
* Output: true
8+
* <p>
9+
* 解题思路:快慢指针
10+
*/
11+
public class _141_linked_list_cycle {
12+
public boolean hasCycle(ListNode head) {
13+
if (head == null || head.next == null) {
14+
return false;
15+
}
16+
17+
ListNode slow = head;
18+
ListNode fast = head.next;
19+
20+
while (slow != fast) {
21+
if (fast == null || fast.next == null) {
22+
return false;
23+
}
24+
25+
fast = fast.next.next;
26+
slow = slow.next;
27+
}
28+
29+
return true;
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Remove all elements from a linked list of integers that have value val.
5+
* Input: 1->2->6->3->4->5->6, val = 6
6+
* Output: 1->2->3->4->5
7+
*
8+
* 解题思路:由于头节点也是有可能被删除的,在头节点前面在构造一个节点
9+
*/
10+
public class _203_remove_linked_list_elements {
11+
public ListNode removeElements(ListNode head, int val) {
12+
if (head == null) {
13+
return null;
14+
}
15+
16+
ListNode listNode = new ListNode(-1);
17+
listNode.next = head;
18+
19+
ListNode tmp = listNode;
20+
21+
while (tmp.next != null) {
22+
if (tmp.next.val == val) {
23+
tmp.next = tmp.next.next;
24+
continue;
25+
}
26+
27+
tmp = tmp.next;
28+
}
29+
30+
return listNode.next;
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Reverse a singly linked list.
5+
* Input: 1->2->3->4->5->NULL
6+
* Output: 5->4->3->2->1->NULL
7+
*/
8+
public class _206_reverse_linked_list {
9+
public ListNode reverseList(ListNode head) {
10+
if (head == null) {
11+
return null;
12+
}
13+
ListNode prev = null;
14+
ListNode curr = head, tmp = curr.next;
15+
16+
while (tmp != null) {
17+
curr.next = prev;
18+
prev = curr;
19+
curr = tmp;
20+
tmp = tmp.next;
21+
}
22+
23+
curr.next = prev;
24+
25+
return curr;
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Merge two sorted linked lists and return it as a new list.
5+
* The new list should be made by splicing together the nodes of the first two lists.
6+
* <p>
7+
* 来源:力扣(LeetCode)
8+
* 链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
9+
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
10+
* <p>
11+
* Input: 1->2->4, 1->3->4
12+
* Output: 1->1->2->3->4->4
13+
*/
14+
public class _21_merge_two_sorted_lists {
15+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
16+
if (l1 == null) {
17+
return l2;
18+
}
19+
20+
if (l2 == null) {
21+
return l1;
22+
}
23+
24+
ListNode ln = new ListNode(-1);
25+
ListNode prev = ln;
26+
27+
while (l1 != null && l2 != null) {
28+
if (l1.val <= l2.val) {
29+
prev.next = l1;
30+
l1 = l1.next;
31+
} else {
32+
prev.next = l2;
33+
l2 = l2.next;
34+
}
35+
36+
prev = prev.next;
37+
}
38+
39+
prev.next = l1 == null ? l2 : l1;
40+
41+
return ln.next;
42+
}
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
*Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
5+
*
6+
* Given linked list -- head = [4,5,1,9]
7+
*
8+
* 来源:力扣(LeetCode)
9+
* 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list
10+
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
11+
*/
12+
public class _237_delete_node_in_a_linked_list {
13+
public void deleteNode(ListNode node) {
14+
node.val = node.next.val;
15+
node.next = node.next.next;
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Given a sorted linked list, delete all duplicates such that each element appear only once.
5+
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
6+
* Input: 1->1->2
7+
* Output: 1->2
8+
*/
9+
public class _83_remove_duplicates_from_sorted_list {
10+
11+
public ListNode deleteDuplicates(ListNode head) {
12+
if (head == null) {
13+
return null;
14+
}
15+
16+
ListNode tmp = head;
17+
int val = tmp.val;
18+
19+
while (tmp != null) {
20+
if (tmp.next != null && tmp.next.val == val) {
21+
tmp.next = tmp.next.next;
22+
continue;
23+
}
24+
25+
if (tmp.next != null) {
26+
val = tmp.next.val;
27+
}
28+
29+
tmp = tmp.next;
30+
}
31+
32+
return head;
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fs.leetcode.linkedlist;
2+
3+
/**
4+
* Given a non-empty, singly linked list with head node head, return a middle node of linked list.
5+
* If there are two middle nodes, return the second middle node.
6+
*
7+
* 解题思路:快慢指针
8+
*/
9+
public class _876_middle_of_the_linked_list {
10+
public ListNode middleNode(ListNode head) {
11+
if (head == null || head.next == null) {
12+
return head;
13+
}
14+
15+
ListNode slow = head;
16+
ListNode fast = head.next;
17+
18+
while (fast != null) {
19+
if (fast.next == null) {
20+
slow = slow.next;
21+
break;
22+
}
23+
24+
fast = fast.next.next;
25+
slow = slow.next;
26+
}
27+
28+
return slow;
29+
}
30+
31+
public static void main(String[] args) {
32+
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.