algorithm - Given a linked list of numbers. Swap every 2 adjacent links -


Swap every 2 adjacent links in view of the linked list of numbers. For example, if you have a linked list given :

  a-> gt; B-> c-> gt; D-> e-> f  

Expected output:

  b-> a-> gtc- gt; C- & gt; F- & gt; E  

will have to swap every 2 optional links.

I have written a solution here. Can you recommend me some other solutions Can you comment on your solution and help me write it better?

  zero swapjetentode (node ​​head) {return (head == blank); If (head.next == faucet) return; Node curr = head; Node next = curr.Next; Node temp = next next; While (true) {temp = next.Next; Next Next = curr; Curr.Next = temp; If (curr.Next! = Null) curr = curr.Next; And break; If (curr.Next. Next! = Faucet) Next = curr.Next. Next; And break; }}  

Take a look at this C ++ solution:

< Pre class = "Lang-c prettyprint-override"> Public Zero Exchanges Engels () {Llme backup = current. Llmien temp = current.NET; LLMN Previous = Current; While (current! = Null & present. Newext = null) {previous.NX = present. Current.next = temp.next; Temp.next = current; If (current.nax! = Null) {previous = current; Current = current.next; Temporary = current.next; }} Current = backup; }

Here is the head node at present.


Comments