Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of nodes in a pairwise exchange list

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the node example in the pairwise exchange list". The explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn the node example in the pairwise exchange list.

Solve the problem by yourself for the first time (timed out)

Public ListNode swapPairs (ListNode head) {

ListNode curr = head

While (curr! = null) {

ListNode next = curr.next.next

If (next! = null) {

ListNode temp = curr

Curr = next

Curr.next.next = temp

}

}

Return curr

}

Solve the problem for the second time (timeout error)

Public ListNode swapPairs (ListNode head) {

ListNode curr = head

ListNode pre = head.next

While (curr! = null & & pre! = null) {

ListNode a = curr

ListNode b = curr.next

/ / modify the pointer of the current node to the previous node

A.next = pre

Pre.next = b

Curr = b.next

Pre = b

}

Return pre

}

Read the answer after the analysis

Public ListNode swapPairs (ListNode head) {/ / create an empty node to point to the header pointer ListNode pre = new ListNode (0) by default; pre.next = head; / / temp points to the first node ListNode temp = pre; while (temp.next! = null & & temp.next.next! = null) {/ / the element to be exchanged ListNode a = temp.next / / the next element to be exchanged is ListNode b = temp.next.next; / / temp pointing to b temp.next = b; / a pointing to b.next a.next = b.next.b pointing to a b.next = a; / / turning the exchanged a node into the starting node temp = a;} / /? This return value still does not understand skipping return pre.next;} Thank you for your reading. The above is the content of the "example of nodes in the pairwise exchange list". After the study of this article, I believe you have a deeper understanding of the example of nodes in the pairwise exchange list, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report