标签:
Reverse Linked List
题目大意:把当前的linked list顺序颠倒
思路: 1. a,b交换值 a=temp
temp = b
b = a
2.用一个while循环,不断把当前拿到的值放在新的linked list的头上
3.注意循环结束条件和指针的变化
代码:
1 public ListNode reverse(ListNode head) { 2 if (head == null) { 3 return head; 4 } 5 ListNode current = new ListNode(0); 6 ListNode temp = new ListNode(0); 7 ListNode pre = new ListNode(0); 8 9 pre = null; 10 current = head; 11 while (current != null) { 12 temp = current.next; 13 current.next = pre; 14 pre = current; 15 current = temp; 16 } 17 return pre; 18 } 19 }
注意点: 1. 原有的linked list最前面加一个null节点
2. 循环条件为current != null
3.返回值为pre pointer 而不是current pointer
标签:
原文地址:http://www.cnblogs.com/jiangchen/p/5397555.html