-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion148.java
More file actions
78 lines (71 loc) · 1.36 KB
/
Copy pathQuestion148.java
File metadata and controls
78 lines (71 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 148
*/
public class Question148
{
public static ListNode sortList(ListNode head)
{
if(head==null || head.next==null)
return head;
ListNode temp = head;
int length = 0;
while(temp!=null)
{
length++;
temp = temp.next;
}
ListNode pointer1,pointer2;
ListNode start = head;
ListNode temp11=null;
boolean flag = true;
for(int i = 0;i<length;i++)
{
temp = start;
flag = true;
while(temp.next!=null)
{
pointer1 = temp;
pointer2 = temp.next;
if(pointer1.val>pointer2.val)
{
if(pointer1 == start)
{
pointer1.next = pointer2.next;
pointer2.next = pointer1;
if(flag)
{
flag = false;
start = pointer2;
}
}
else
{
pointer1.next = pointer2.next;
pointer2.next = pointer1;
temp11.next = pointer2;
if(flag)
flag = false;
}
temp = pointer2;
}
else
{
if(flag)
flag = false;
}
temp11 = temp;
temp = temp.next;
}
}
return start;
}
public static void main(String[] args)
{
ListNode head = LinkedList.create(7,"unsorted");
LinkedList.display(head);
ListNode result = sortList(head);
LinkedList.display(result);
}
}