File tree Expand file tree Collapse file tree 1 file changed +5
-11
lines changed
src/main/java/com/fishercoder/common/classes Expand file tree Collapse file tree 1 file changed +5
-11
lines changed Original file line number Diff line number Diff line change @@ -52,28 +52,22 @@ public static ListNode createSinglyLinkedList() {
52
52
return head ;
53
53
}
54
54
55
- /**
56
- * TODO: this function is NOT working as supposed to, I need to fix it! Commit from my Windows machine!
57
- */
58
55
public static ListNode createSinglyLinkedList (List <Integer > listValues ) {
59
56
if (listValues == null || listValues .size () == 0 ) {
60
57
throw new IllegalArgumentException (
61
58
"Please pass in a valid listValues to create a singly linked list." );
62
59
}
63
60
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 ;
66
66
}
67
67
printList (head );
68
68
return head ;
69
69
}
70
70
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
-
77
71
public static void printList (ListNode head ) {
78
72
ListNode temp = head ;
79
73
System .out .println ();
You can’t perform that action at this time.
0 commit comments