Quantcast
Channel: Singly Linked List "Strange sorting" exercise - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Singly Linked List "Strange sorting" exercise

$
0
0

I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there.

One of these involves taking a singly linked list (type not specified) and rearranging the nodes. The sorting involves first having all nodes appear that were at "even" positions in the prior linked list and after that having all nodes appear that were at "uneven" positions in the prior linked list. All this while every node should keep their relative position. I chose to write this for a linked list with a null reference at its tail and a true node as head.

So short example: a list of values 4-5-7-3-2-1-null should rearrange to4-7-2-5-3-1-null.

I wrote some code that worked (the strangeSort (Node x) method) but can't think of it as an elegant solution. I am convinced that there must be a way out there to make this neater, especially the for-loop in the strangeSort method. Base idea behind it was to make the original linked list basically 2 linked lists with different head nodes and then attaching one linked list at the end of the other.

The code:

public class Test {    static class Node    {        int val; Node next;        Node(int v){val=v;}    }    public static void main (String[] args)    {        /*Create Linked list of length N*/        int N=Integer.parseInt(args[0]);        Node head = new Node(0);        Node x = head, x2;        for (int i=1; i<N; i++, x = x2)        {            x2 = new Node(i);            x.next = x2;        }        /*Prints out linked list for checking purposes*/        for (Node t = head; t!=null; t=t.next)        {               System.out.print(t.val+" -> ");        }        /*Rearrange Linked list*/        strangeSort(head);        /*Prints out linked list after sorting for checking purposes*/        System.out.println();        for (Node t = head; t!=null; t=t.next)        {               System.out.print(t.val+" -> ");        }    }    public static void strangeSort (Node x)    {        Node headu = x, heade = x.next;        Node t,s;        for (t=headu, s=heade ; t!=null && s!=null ; t=t.next , s=s.next)        {            if (t.next==null){break;}            t.next = t.next.next;            if (s.next ==null){break;}            s.next = s.next.next;        }        if (t==null){t=heade;}        else if (t.next==null){t.next = heade;}    }}

As you can see, the forloop of strangeSort contains a fairly inelegant way to make sure I don't get a NullPointerException in the end through the fact that I am traversing 2 nodes every iteration of the loop.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>