title: Rearranging a linked list
body: <p>I'm trying to solve the following problem </p>
  
  <blockquote>
    <p>Write a method split that rearranges the elements of a list so that all of the negative values appear before all of the non-negatives. 
        For example, suppose a variable list stores the following sequence of values:</p>
  </blockquote>
  
  <pre><code>[8, 7, -4, 19, 0, 43, -8, -7, 2]
  </code></pre>
  
  <blockquote>
    <p>The call of list.split(); should rearrange the list to put the negatives first. One possible arrangement would be the following:</p>
  </blockquote>
  
  <pre><code>[-4, -8, -7, 8, 7, 19, 0, 43, 2]
  </code></pre>
  
  <blockquote>
    <p>But it matters only that the negatives appear before the non-negatives. 
        So this is only one possible solution. Another legal solution would be to rearrange the values this way:</p>
  </blockquote>
  
  <pre><code>[-7, -8, -4, 2, 43, 0, 19, 7, 8]
  </code></pre>
  
  <blockquote>
    <p>You are not allowed to swap data fields or to create any new nodes to solve this problem; 
        you must rearrange the list by rearranging the links of the list. 
        You also may not use auxiliary structures like arrays, ArrayLists, stacks, queues, etc, to solve this problem.</p>
  </blockquote>
  
  <p>I'm really clueless as how to solve this problem. One approach which I'm thinking of is to identify nodes with negative data and add it to the head, but I'm unable to figure out how to code this approach. Here is my list class.</p>
  
  <pre><code>public class LinkedList {
  
  // The Linked List's Node structure
     static class Node {
      private Node next;
      private int data;
  
      Node(int data) {
          this.data = data;
          next = null;
      }
  
      public int getData() {
          return data;
      }
  
      public void setNext(Node next) {
          this.next = next;
      }
  
      public void setData(int data) {
          this.data = data;
      }
  
      public Node getNext() {
          return next;
      }
  
  }
  
  private Node head;
  private int size;
  
  public void setHead(Node head) {
      this.head = head;
  }
  
  public int getSize() {
      return size;
  }
  
  public Node getHead() {
      return head;
  }
  
  public LinkedList() {
      head = null;
      size = 0;
  }
  }
  </code></pre>
  
  <p>Any hints/suggestion for tackling this problem?</p>
  
  <p>As suggested by @nishu I have come up with the following solution. It works.</p>
  
  <pre><code>public Node delete(int data) {
  
      Node del = null;
      Node tmp = head;
      if (head.getData() == data) {
          Node tmp2 = head;
          Node nxt = tmp.getNext();
          tmp2.setNext(null);
          this.setHead(nxt);
          del = tmp2;
      } else if (isLast(data)) {
          while (true) {
              if (tmp.getNext().getNext() == null) {
                  del = tmp.getNext();
                  tmp.setNext(null);
                  break;
              }
              tmp = tmp.getNext();
          }
      } else {
          while (tmp != null &amp;&amp; tmp.getNext() != null) {
              if (tmp.getNext().getData() == data) {
                  Node prev = tmp;
                  del = tmp.getNext();
                  Node nextOfToBeDeleted = tmp.getNext().getNext();
                  prev.setNext(nextOfToBeDeleted);
                  break;
              }
              tmp = tmp.getNext();
          }
      }
      return del;
  }
  
  public void addToFront(Node node) {
      node.setNext(head);
      this.setHead(node);
  }
  
  public void split() {
  
      Node tmp = head;
      Node del = null;
      while (tmp != null) {
          if (tmp.getData() &lt; 0) {
              Node nxt = tmp.getNext();
              del = delete(tmp.getData());
              addToFront(del);
              while (tmp != nxt) {
                  tmp = tmp.getNext();
              }
          } else {
              tmp = tmp.getNext();
          }
  
      }
  }
  
  public boolean isLast(int data) {
      boolean last = false;
      Node tmp = head;
      while (true) {
          if (tmp.getData() == data &amp;&amp; tmp.getNext() != null) {
              last = false;
              break;
          } else if (tmp.getNext() == null &amp;&amp; tmp.getData() == data) {
              last = true;
              break;
          }
          tmp = tmp.getNext();
      }
      return last;
  }
  </code></pre>