数据结构练习

题目

反转链表:https://leetcode.com/problems/reverse-linked-list/

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

public class ReverseLinkedList {

public class ListNode {
int val;
ListNode next;

ListNode(int x) {
val = x;
}
}

@Test
public void test1() {

ListNode a = new ListNode(1);
ListNode b = new ListNode(2);
ListNode c = new ListNode(3);
ListNode d = new ListNode(4);
ListNode e = new ListNode(5);

a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = null;

ListNode listNode = reverseList2(a);
}

//迭代
public ListNode reverseList2(ListNode head) {
//判断是链表是否为空或者只有一个值的列表
ListNode pre = null, cur = head;
while (cur != null){
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}

}