Skip to content

Commit 2c80c2d

Browse files
fix LinkedList utils
1 parent f152997 commit 2c80c2d

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/main/java/com/fishercoder/common/classes/ListNode.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,22 @@ public static ListNode createSinglyLinkedList() {
5252
return head;
5353
}
5454

55-
/**
56-
* TODO: this function is NOT working as supposed to, I need to fix it! Commit from my Windows machine!
57-
*/
5855
public static ListNode createSinglyLinkedList(List<Integer> listValues) {
5956
if (listValues == null || listValues.size() == 0) {
6057
throw new IllegalArgumentException(
6158
"Please pass in a valid listValues to create a singly linked list.");
6259
}
6360
ListNode head = new ListNode(listValues.get(0));
64-
for (int i : listValues) {
65-
appendNode(head, i);
61+
ListNode tmp = head;
62+
for (int i = 1; i < listValues.size(); i++) {
63+
ListNode next = new ListNode(listValues.get(i));
64+
tmp.next = next;
65+
tmp = tmp.next;
6666
}
6767
printList(head);
6868
return head;
6969
}
7070

71-
private static void appendNode(ListNode head, int i) {
72-
ListNode node = new ListNode(i);
73-
head.next = node;
74-
// head = head.next;
75-
}
76-
7771
public static void printList(ListNode head) {
7872
ListNode temp = head;
7973
System.out.println();

0 commit comments

Comments
 (0)