title: Bubble sort algorithm for a linked list
body: <p>I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly.</p>
  <pre class="lang-java prettyprint-override"><code>class SListNode {
      Object item;
      SListNode next;
  
      SListNode(Object obj) {
          item = obj;
          next = null;
      }
  
      SListNode(Object obj, SListNode next) {
          item = obj;
          this.next = next;
      }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public class SList {
      private SListNode head;
      private SListNode temp;
  
      public void sortList() {
          SListNode node = head, i, j;
          head = node;
          i = node;
          j = node.next;
          while (i.next != null) {
              while (j.next != null) {
                  if ((Integer) i.item &lt; (Integer) j.item) {
                      temp = i.next;
                      i.next = j.next;
                      j.next = temp;
                  }
                  j = j.next;
              }
              i = i.next;
          }
      }
  }
  </code></pre>
  <p>This is the output I am getting</p>
  <pre><code>List after construction: [  3  6  9  4  12  15  ]
  After sorting: [  3  4  9  12  6  15  ]
  </code></pre>
  <p>Besides I know the worst case scenario of a bubble sort is O(n<sup>2</sup>). Can I use mergesort on a linked list to have a better time complexity?</p>