Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit424fcb8

Browse files
author
applewjg
committed
Insertion Sort List
Change-Id: I761fa5981176f1100aa0d622dbf05eadd89e4501
1 parent09ec18b commit424fcb8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎InsertionSortList.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Jan 02, 2015
4+
Problem: Insertion Sort List
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/insertion-sort-list/
7+
Notes:
8+
Sort a linked list using insertion sort.
9+
10+
Solution: ...
11+
*/
12+
/**
13+
* Definition for singly-linked list.
14+
* public class ListNode {
15+
* int val;
16+
* ListNode next;
17+
* ListNode(int x) {
18+
* val = x;
19+
* next = null;
20+
* }
21+
* }
22+
*/
23+
publicclassSolution {
24+
publicListNodeinsertionSortList(ListNodehead) {
25+
if (head ==null ||head.next ==null)returnhead;
26+
ListNodedummy =newListNode(Integer.MIN_VALUE);
27+
dummy.next =head;
28+
ListNodecur =head.next;
29+
head.next =null;
30+
while (cur !=null) {
31+
ListNodetmp =dummy;
32+
while (tmp.next !=null &&tmp.next.val <=cur.val)tmp =tmp.next;
33+
ListNodenext =cur.next;
34+
cur.next =tmp.next;
35+
tmp.next =cur;
36+
cur =next;
37+
}
38+
returndummy.next;
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp