Skip to content

Commit 4b4194a

Browse files
committed
Added 8 linked list solutions
1 parent a2701fa commit 4b4194a

8 files changed

+272
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
ListNode curr;
20+
21+
public TreeNode sortedListToBST(ListNode head) {
22+
curr = head;
23+
return generate(count(head));
24+
}
25+
26+
public int count(ListNode node) {
27+
int n = 0;
28+
while (node != null) {
29+
node = node.next;
30+
++n;
31+
}
32+
return n;
33+
}
34+
35+
public TreeNode generate(int n) {
36+
if (n==0) return null;
37+
38+
TreeNode node = new TreeNode(0);
39+
node.left = generate(n/2);
40+
node.val = curr.val;
41+
curr = curr.next;
42+
node.right = generate(n-(n/2)-1);
43+
44+
return node;
45+
}
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list with a random pointer.
3+
* class RandomListNode {
4+
* int label;
5+
* RandomListNode next, random;
6+
* RandomListNode(int x) { this.label = x; }
7+
* };
8+
*/
9+
public class Solution {
10+
public RandomListNode copyRandomList(RandomListNode head) {
11+
if (head == null) return head;
12+
13+
Map<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>();
14+
15+
RandomListNode curr = head;
16+
while (curr != null) {
17+
map.put(curr,new RandomListNode(curr.label));
18+
curr = curr.next;
19+
}
20+
21+
for (Map.Entry<RandomListNode, RandomListNode> entry : map.entrySet()) {
22+
RandomListNode n = entry.getValue();
23+
n.next = map.get(entry.getKey().next);
24+
n.random = map.get(entry.getKey().random);
25+
}
26+
27+
return map.get(head);
28+
}
29+
}

Medium/Insertion Sort List.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode insertionSortList(ListNode head) {
11+
ListNode temp = head;
12+
while (temp != null) {
13+
ListNode curr =temp;
14+
int v = curr.val;
15+
ListNode t = temp;
16+
while (curr != null) {
17+
if (curr.val < v) {
18+
v = curr.val;
19+
t = curr;
20+
}
21+
curr = curr.next;
22+
}
23+
t.val = temp.val;
24+
temp.val = v;
25+
temp = temp.next;
26+
}
27+
return head;
28+
}
29+
}

Medium/Partition List.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode partition(ListNode head, int x) {
11+
Queue<Integer> small = new LinkedList<>();
12+
Queue<Integer> bigOrEqual = new LinkedList<>();
13+
ListNode curr = head;
14+
while (curr != null){
15+
if (curr.val < x) {
16+
small.add(curr.val);
17+
}
18+
else {
19+
bigOrEqual.add(curr.val);
20+
}
21+
curr = curr.next;
22+
}
23+
24+
curr = head;
25+
while (curr != null) {
26+
if (!small.isEmpty()) {
27+
curr.val = small.remove();
28+
}
29+
else {
30+
curr.val = bigOrEqual.remove();
31+
}
32+
curr = curr.next;
33+
}
34+
35+
return head;
36+
}
37+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode deleteDuplicates(ListNode head) {
11+
if(head==null) return null;
12+
13+
ListNode FakeHead=new ListNode(0);
14+
FakeHead.next=head;
15+
ListNode pre=FakeHead;
16+
ListNode cur=head;
17+
18+
while(cur!=null){
19+
while(cur.next!=null&&cur.val==cur.next.val){
20+
cur=cur.next;
21+
}
22+
if(pre.next==cur){
23+
pre=pre.next;
24+
}
25+
else{
26+
pre.next=cur.next;
27+
}
28+
cur=cur.next;
29+
}
30+
return FakeHead.next;
31+
}
32+
}

Medium/Reorder List.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public void reorderList(ListNode head) {
11+
if (head == null || head.next == null) return;
12+
ArrayList<Integer> arr = new ArrayList<>();
13+
ListNode temp = head;
14+
while(temp != null) {
15+
arr.add(temp.val);
16+
temp = temp.next;
17+
}
18+
19+
temp = head;
20+
int i=0;
21+
int j=arr.size()-1;
22+
while (temp != null) {
23+
temp.val = arr.get(i);
24+
if (temp.next != null) {
25+
temp.next.val = arr.get(j);
26+
temp = temp.next.next;
27+
}
28+
else {
29+
temp = temp.next;
30+
}
31+
i++;
32+
j--;
33+
34+
}
35+
}
36+
}

Medium/Reverse Linked List II.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode reverseBetween(ListNode head, int m, int n) {
11+
ArrayList<Integer> arr = new ArrayList<>();
12+
ListNode curr = head;
13+
while (curr != null) {
14+
arr.add(curr.val);
15+
curr = curr.next;
16+
}
17+
18+
curr = head;
19+
int i=1;
20+
int j=n-1;
21+
while(curr != null) {
22+
if (i>=m && i<=n) {
23+
curr.val = arr.get(j);
24+
j--;
25+
}
26+
curr = curr.next;
27+
i++;
28+
}
29+
return head;
30+
}
31+
}

Medium/Reverse Nodes in k-group.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode reverseKGroup(ListNode head, int k) {
11+
ArrayList<Integer> arr = new ArrayList<>();
12+
ListNode curr = head;
13+
while (curr != null) {
14+
arr.add(curr.val);
15+
curr = curr.next;
16+
}
17+
18+
curr = head;
19+
int i = 0;
20+
int n = k;
21+
while(i+k <= arr.size()) {
22+
while(n != 0) {
23+
curr.val = arr.get(i+n-1);
24+
curr = curr.next;
25+
n--;
26+
}
27+
i += k;
28+
n = k;
29+
}
30+
return head;
31+
}
32+
}

0 commit comments

Comments
 (0)