Singly linked list reversal is a common interview questions

Transfer from https://www.cnblogs.com/byrhuangqiang/p/4311336.html

Singly linked list reversal is a common interview questions. This paper summarizes the two methods.

Definition 1

Single linked node data structure definition is as follows:

Copy the code
Copy the code
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
Copy the code
Copy the code

2 Method 1: reversal situ

2.1 ideas

Next node to the next node in the list pCur currently inserted into the head of the dummy node, in situ reverse.

dummy-> 1-> 2-> 3-> 4-> 5 situ reversing process:

dummy->2->1->3->4->5
dummy->3->2->1->4->5
dummy->4>-3->2->1->5
dummy->5->4->3->2->1

2.2 explanation

An initial state

Process 2

pCur is a need to reverse the node.

  1. Prev next time connections requiring reversal node
  2. Inverted node pCur
  3. Correcting the first node pointing dummy
  4. pCur point to the next node to be reversed

Fake code

1 prev.next = pCur.next;
2 pCur.next = dummy.next;
3 dummy.next = pCur;
4 pCur = prev.next;

3 cycling conditions

pCur is not null

2.3 Code

Copy the code
Copy the code
 1 // 1. situ reversal
 2     public ListNode reverseList1(ListNode head) {
 3         if (head == null)
 4             return head;
 5         ListNode dummy = new ListNode(-1);
 6         dummy.next = head;
 7         ListNode prev = dummy.next;
 8         ListNode pCur = prev.next;
 9         while (pCur != null) {
10             prev.next = pCur.next;
11             pCur.next = dummy.next;
12             dummy.next = pCur;
13             pCur = prev.next;
14         }
15         return dummy.next;
16     }
Copy the code
Copy the code

2.4 总结

  • 1个头结点,2个指针,4行代码
  • 注意初始状态和结束状态,体会中间的图解过程。

 

3 方法2:新建链表,头节点插入法

3.1 思路

新建一个头结点,遍历原链表,把每个节点用头结点插入到新建链表中。最后,新建的链表就是反转后的链表。

3.2 解释

1 初始状态

2 过程

pCur是要插入到新链表的节点。

pNex是临时保存的pCur的next。

  1. pNex保存下一次要插入的节点
  2. 把pCur插入到dummy中
  3. 纠正头结点dummy的指向
  4. pCur指向下一次要插入的节点

伪代码

1 pNex = pCur.next
2 pCur.next = dummy.next
3 dummy.next = pCur
4 pCur = pNex

3 循环条件

pCur is not null

3.3 代码

Copy the code
Copy the code
 1     // 2.新建链表,头节点插入法
 2     public ListNode reverseList2(ListNode head) {
 3         ListNode dummy = new ListNode(-1);
 4         ListNode pCur = head;
 5         while (pCur != null) {
 6             ListNode pNex = pCur.next;
 7             pCur.next = dummy.next;
 8             dummy.next = pCur;
 9             pCur = pNex;
10         }
11         return dummy.next;
12     }
Copy the code
Copy the code

3.4 总结

  • 1个头结点,2个指针(包含一个临时保存节点的pNex),4行代码
  • 注意初始状态和结束状态,体会中间的图解过程。
 

单链表的反转是常见的面试题目。本文总结了2种方法。

1 定义

单链表node的数据结构定义如下:

Copy the code
Copy the code
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
Copy the code
Copy the code

2 方法1:就地反转法

2.1 思路

把当前链表的下一个节点pCur插入到头结点dummy的下一个节点中,就地反转。

dummy->1->2->3->4->5的就地反转过程:

dummy->2->1->3->4->5
dummy->3->2->1->4->5
dummy->4>-3->2->1->5
dummy->5->4->3->2->1

2.2 解释

1初始状态

2 过程

pCur是需要反转的节点。

  1. prev连接下一次需要反转的节点
  2. 反转节点pCur
  3. 纠正头结点dummy的指向
  4. pCur指向下一次要反转的节点

伪代码

1 prev.next = pCur.next;
2 pCur.next = dummy.next;
3 dummy.next = pCur;
4 pCur = prev.next;

3 循环条件

pCur is not null

2.3 代码

Copy the code
Copy the code
 1     // 1.就地反转法
 2     public ListNode reverseList1(ListNode head) {
 3         if (head == null)
 4             return head;
 5         ListNode dummy = new ListNode(-1);
 6         dummy.next = head;
 7         ListNode prev = dummy.next;
 8         ListNode pCur = prev.next;
 9         while (pCur != null) {
10             prev.next = pCur.next;
11             pCur.next = dummy.next;
12             dummy.next = pCur;
13             pCur = prev.next;
14         }
15         return dummy.next;
16     }
Copy the code
Copy the code

2.4 总结

  • 1个头结点,2个指针,4行代码
  • 注意初始状态和结束状态,体会中间的图解过程。

 

3 方法2:新建链表,头节点插入法

3.1 思路

新建一个头结点,遍历原链表,把每个节点用头结点插入到新建链表中。最后,新建的链表就是反转后的链表。

3.2 解释

1 初始状态

2 过程

pCur是要插入到新链表的节点。

pNex是临时保存的pCur的next。

  1. pNex保存下一次要插入的节点
  2. 把pCur插入到dummy中
  3. 纠正头结点dummy的指向
  4. pCur指向下一次要插入的节点

伪代码

1 pNex = pCur.next
2 pCur.next = dummy.next
3 dummy.next = pCur
4 pCur = pNex

3 cycling conditions

pCur is not null

3.3 Code

Copy the code
Copy the code
 1 // 2. Create the list, the first node insertion method
 2     public ListNode reverseList2(ListNode head) {
 3         ListNode dummy = new ListNode(-1);
 4         ListNode pCur = head;
 5         while (pCur != null) {
 6             ListNode pNex = pCur.next;
 7             pCur.next = dummy.next;
 8             dummy.next = pCur;
 9             pCur = pNex;
10         }
11         return dummy.next;
12     }
Copy the code
Copy the code

3.4 summary

  • 1 head node, two pointers (comprising a temporary storage node pNex), line 4
  • Note that the initial state and the end state, the intermediate experience illustrates the process.

Guess you like

Origin www.cnblogs.com/qinaidexin/p/11819509.html