Today, we implement a sorted linked-list and try to feel how to write beautiful code. Question Implement a sorted linked-list with max possible value known and following declaration: Class SortedList { public SortedList ( int maxValue) { //... } public void insert ( int i) { } } Simple Version What comes to my mind, at the beginning, is some code like the following: public SortedList0 ( int max) { } public void insert ( int i) { if (head == null ) { head = new ListNode(i, null ); } else { ListNode prev = null , next = head; while (next != null && next.getVal() < i) { prev = next; next = next.getNext(); } if (prev == null ) { head = new ListNode(i, head); } else { prev.setNext( new ListNode(i, next)); } } } This version works, but we can see there are some special cases need to handle which make o...
Learn programming, still on the way