Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c5071d7

Browse files
committed
Add one to linked list: done
1 parent 51d8742 commit c5071d7

File tree

10 files changed

+167
-69
lines changed

10 files changed

+167
-69
lines changed

‎src/main/java/com/ctci/linkedlists/DeleteMiddleNode.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ctci.linkedlists;
22

3-
import static com.ctci.linkedlists.Node.printList;
4-
53
/**
64
* @author rampatra
75
* @since 2019年01月27日
@@ -35,33 +33,33 @@ public static void main(String[] args) {
3533
l1.next.next.next = new Node(4);
3634
l1.next.next.next.next = new Node(5);
3735
l1.next.next.next.next.next = new Node(6);
38-
printList(l1);
36+
l1.print();
3937
deleteMiddleNode(l1.next.next);
40-
printList(l1);
38+
l1.print();
4139

4240
System.out.println("----");
4341

4442
l1 = new Node(1);
4543
l1.next = new Node(2);
4644
l1.next.next = new Node(3);
47-
printList(l1);
45+
l1.print();
4846
deleteMiddleNode(l1.next);
49-
printList(l1);
47+
l1.print();
5048

5149
System.out.println("----");
5250

5351
l1 = new Node(1);
5452
l1.next = new Node(3);
55-
printList(l1);
53+
l1.print();
5654
deleteMiddleNode(l1);
57-
printList(l1);
55+
l1.print();
5856

5957
System.out.println("----");
6058

6159
l1 = new Node(1);
6260
l1.next = new Node(3);
63-
printList(l1);
61+
l1.print();
6462
deleteMiddleNode(l1.next);
65-
printList(l1);
63+
l1.print();
6664
}
6765
}

‎src/main/java/com/ctci/linkedlists/Intersection.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ctci.linkedlists;
22

3-
import static com.ctci.linkedlists.Node.printList;
4-
53
/**
64
* @author rampatra
75
* @since 2019年02月02日
@@ -75,16 +73,16 @@ public static void main(String[] args) {
7573
l2.next.next = new Node(2);
7674
l2.next.next.next = new Node(3);
7775
l2.next.next.next.next = l1.next.next.next;
78-
printList(l1);
79-
printList(l2);
76+
l1.print();
77+
l2.print();
8078
System.out.println(findIntersectingNode(l1, l2).val); // may throw NPE, not handling for the sake of simplicity
8179

8280
System.out.println("----");
8381

8482
l1 = new Node(1);
8583
l2 = l1;
86-
printList(l1);
87-
printList(l2);
84+
l1.print();
85+
l2.print();
8886
System.out.println(findIntersectingNode(l1, l2).val); // may throw NPE, not handling for the sake of simplicity
8987

9088
System.out.println("----");
@@ -99,8 +97,8 @@ public static void main(String[] args) {
9997
l2.next = new Node(4);
10098
l2.next.next = new Node(2);
10199
l2.next.next.next = new Node(3);
102-
printList(l1);
103-
printList(l2);
100+
l1.print();
101+
l2.print();
104102
System.out.println(findIntersectingNode(l1, l2));
105103
}
106104
}

‎src/main/java/com/ctci/linkedlists/KthToLastElement.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ctci.linkedlists;
22

3-
import static com.ctci.linkedlists.Node.printList;
4-
53
/**
64
* @author rampatra
75
* @since 21/11/2018
@@ -60,7 +58,7 @@ public static void main(String[] args) {
6058
l1.next.next.next = new Node(4);
6159
l1.next.next.next.next = new Node(5);
6260
l1.next.next.next.next.next = new Node(7);
63-
printList(l1);
61+
l1.print();
6462
System.out.println("k=2: " + getKthToLastElement(l1, 2).val); // NPE check is omitted intentionally to keep it simple
6563
System.out.print("k=2: ");
6664
printKthToLastElement(l1, 2);
@@ -71,7 +69,7 @@ public static void main(String[] args) {
7169
l2.next.next.next = new Node(3);
7270
l2.next.next.next.next = new Node(4);
7371
l2.next.next.next.next.next = new Node(7);
74-
printList(l2);
72+
l2.print();
7573
System.out.println("k=1: " + getKthToLastElement(l2, 1).val);
7674
System.out.print("k=1: ");
7775
printKthToLastElement(l2, 1);
@@ -82,7 +80,7 @@ public static void main(String[] args) {
8280
l3.next.next.next = new Node(3);
8381
l3.next.next.next.next = new Node(4);
8482
l3.next.next.next.next.next = new Node(7);
85-
printList(l3);
83+
l3.print();
8684
System.out.println("k=6: " + getKthToLastElement(l3, 6).val);
8785
System.out.print("k=6: ");
8886
printKthToLastElement(l3, 6);

‎src/main/java/com/ctci/linkedlists/Node.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class Node {
1212
this.val = val;
1313
}
1414

15-
public static void printList(Node head) {
16-
if (head == null) return;
17-
18-
Node curr = head;
15+
public void print() {
16+
Node curr = this;
1917
while (curr.next != null) {
2018
System.out.print(curr.val + "->");
2119
curr = curr.next;

‎src/main/java/com/ctci/linkedlists/Palindrome.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.util.Stack;
44

5-
import static com.ctci.linkedlists.Node.printList;
6-
75
/**
86
* @author rampatra
97
* @since 2019年02月02日
@@ -75,7 +73,7 @@ public static void main(String[] args) {
7573
l1.next.next.next = new Node(3);
7674
l1.next.next.next.next = new Node(2);
7775
l1.next.next.next.next.next = new Node(1);
78-
printList(l1);
76+
l1.print();
7977
System.out.println(isPalindrome(l1));
8078
System.out.println(isPalindromeOptimized(l1));
8179
System.out.println("------");
@@ -85,7 +83,7 @@ public static void main(String[] args) {
8583
l1.next.next = new Node(3);
8684
l1.next.next.next = new Node(2);
8785
l1.next.next.next.next = new Node(1);
88-
printList(l1);
86+
l1.print();
8987
System.out.println(isPalindrome(l1));
9088
System.out.println(isPalindromeOptimized(l1));
9189
System.out.println("------");
@@ -95,13 +93,13 @@ public static void main(String[] args) {
9593
l1.next.next = new Node(3);
9694
l1.next.next.next = new Node(3);
9795
l1.next.next.next.next = new Node(0);
98-
printList(l1);
96+
l1.print();
9997
System.out.println(isPalindrome(l1));
10098
System.out.println(isPalindromeOptimized(l1));
10199
System.out.println("------");
102100

103101
l1 = new Node(1);
104-
printList(l1);
102+
l1.print();
105103
System.out.println(isPalindrome(l1));
106104
System.out.println(isPalindromeOptimized(l1));
107105

‎src/main/java/com/ctci/linkedlists/Partition.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ctci.linkedlists;
22

3-
import static com.ctci.linkedlists.Node.printList;
4-
53
/**
64
* Write code to partition a linked list around a value x, such that all nodes less than x come before all
75
* nodes greater than or equal to x. If x is contained within the list, the values of x only need to be
@@ -59,36 +57,36 @@ public static void main(String[] args) {
5957
l1.next.next.next.next = new Node(10);
6058
l1.next.next.next.next.next = new Node(2);
6159
l1.next.next.next.next.next.next = new Node(1);
62-
printList(l1);
63-
printList(partition(l1, 5));
60+
l1.print();
61+
l1.print();
6462

6563
System.out.println("----");
6664

6765
l1 = new Node(1);
6866
l1.next = new Node(2);
6967
l1.next.next = new Node(3);
70-
printList(l1);
71-
printList(partition(l1, 2));
68+
l1.print();
69+
l1.print();
7270

7371
System.out.println("----");
7472

7573
l1 = new Node(3);
7674
l1.next = new Node(2);
7775
l1.next.next = new Node(1);
78-
printList(l1);
79-
printList(partition(l1, 2));
76+
l1.print();
77+
l1.print();
8078

8179
System.out.println("----");
8280

8381
l1 = new Node(1);
84-
printList(l1);
85-
printList(partition(l1, 1));
82+
l1.print();
83+
l1.print();
8684

8785
System.out.println("----");
8886

8987
l1 = null;
90-
printList(l1);
91-
printList(partition(l1, 1));
88+
l1.print();
89+
l1.print();
9290

9391
System.out.println("----");
9492
}

‎src/main/java/com/ctci/linkedlists/RemoveDuplicates.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6-
import static com.ctci.linkedlists.Node.printList;
7-
86
/**
97
* @author rampatra
108
* @since 21/11/2018
@@ -44,10 +42,10 @@ public static void main(String[] args) {
4442
l1.next.next.next.next = new Node(5);
4543
l1.next.next.next.next.next = new Node(5);
4644
System.out.print("With dups: ");
47-
printList(l1);
45+
l1.print();
4846
removeDuplicatesFromUnsortedList(l1);
4947
System.out.print("Without dups: ");
50-
printList(l1);
48+
l1.print();
5149

5250
Node l2 = new Node(1);
5351
l2.next = new Node(1);
@@ -56,10 +54,10 @@ public static void main(String[] args) {
5654
l2.next.next.next.next = new Node(4);
5755
l2.next.next.next.next.next = new Node(5);
5856
System.out.print("\nWith dups: ");
59-
printList(l2);
57+
l2.print();
6058
removeDuplicatesFromUnsortedList(l2);
6159
System.out.print("Without dups: ");
62-
printList(l2);
60+
l2.print();
6361

6462
Node l3 = new Node(1);
6563
l3.next = new Node(2);
@@ -68,23 +66,23 @@ public static void main(String[] args) {
6866
l3.next.next.next.next = new Node(4);
6967
l3.next.next.next.next.next = new Node(5);
7068
System.out.print("\nWith dups: ");
71-
printList(l3);
69+
l3.print();
7270
removeDuplicatesFromUnsortedList(l3);
7371
System.out.print("Without dups: ");
74-
printList(l3);
75-
72+
l3.print();
73+
7674
Node l4 = new Node(1);
7775
System.out.print("\nWith dups: ");
78-
printList(l4);
76+
l4.print();
7977
removeDuplicatesFromUnsortedList(l4);
8078
System.out.print("Without dups: ");
81-
printList(l4);
79+
l4.print();
8280

8381
Node l5 = null;
8482
System.out.print("\nWith dups: ");
85-
printList(l5);
83+
l5.print();
8684
removeDuplicatesFromUnsortedList(l5);
8785
System.out.print("Without dups: ");
88-
printList(l5);
86+
l5.print();
8987
}
9088
}

‎src/main/java/com/ctci/linkedlists/SumLists.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.ctci.linkedlists;
22

3-
import static com.ctci.linkedlists.Node.printList;
4-
53
/**
64
* @author rampatra
75
* @since 2019年01月31日
@@ -59,9 +57,9 @@ public static void main(String[] args) {
5957
l2.next = new Node(9);
6058
l2.next.next = new Node(9);
6159

62-
printList(l1);
63-
printList(l2);
64-
printList(sumLists(l1, l2));
60+
l1.print();
61+
l2.print();
62+
sumLists(l1, l2).print();
6563
System.out.println("-----------");
6664

6765
l1 = new Node(9);
@@ -71,18 +69,18 @@ public static void main(String[] args) {
7169
l2.next = new Node(9);
7270
l2.next.next = new Node(9);
7371

74-
printList(l1);
75-
printList(l2);
76-
printList(sumLists(l1, l2));
72+
l1.print();
73+
l2.print();
74+
sumLists(l1, l2).print();
7775
System.out.println("-----------");
7876

7977
l1 = null;
8078
l2 = new Node(9);
8179
l2.next = new Node(9);
8280
l2.next.next = new Node(8);
8381

84-
printList(l1);
85-
printList(l2);
86-
printList(sumLists(l1, l2));
82+
l1.print();
83+
l2.print();
84+
sumLists(l1, l2).print();
8785
}
8886
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /